News for hackers. Stuff that matters.
Re:Modern Javascript: Everything you missed over the last 10... by sandrot on Saturday May 15, 2021 @04:35PM (#27168749)
Try using Array.from(arrayish).forEach() to convert the thing that looks like an array to an actual array. For instance, you can't use map() on a NodeList (document.querySelectorAll returns a NodeList), so you have to use Array.from(NodeList) to first convert it to an array.
Re:Modern Javascript: Everything you missed over the last 10... by dragonwriter on Saturday May 15, 2021 @04:41PM (#27168807)
> Try using Array.from(arrayish).forEach() to convert the thing that looks like an array to an actual array.

Spread syntax is my goto for that:

  [...arrayish].forEach(...)
But, it being JavaScript, there’s probably a subtle distinction in where Array.from(arrayish) works and where [...arrayish] works.
Re:Modern Javascript: Everything you missed over the last 10... by hajile on Sunday May 16, 2021 @12:14AM (#27171181)
There's a very big distinction.

`...foo` requires `foo` to be an iterable (implements the iterator known symbol).

`Array.From()` only requires it to be array-like and not necessarily iterable.

Re:Modern Javascript: Everything you missed over the last 10... by dragonwriter on Sunday May 16, 2021 @11:24AM (#27174289)
Good point. I don't think I've run into many non-iterable arraylikes, but I understand they used to be more common (e.g., a number of standard things that are now specced as iterable arraylikes used to be just the latter) and there’s probably a lot lurking in library/legacy code.