3 ms·
It's a great compilation, but I'd like to have each item to have a link to the related caniuse.com page. Sometimes I can use Array.forEach() and sometimes not,
by qwertox 5y ago
It's a great compilation, but I'd like to have each item to have a link to the related caniuse.com page.
Sometimes I can use Array.forEach() and sometimes not, and I don't know why, even if I'm only using Chrome. So I just use lodash.
- sandrot 5y agoTry 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.
- dragonwriter 5y ago> 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.
- hajile 5y agoThere'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.
- dragonwriter 5y agoGood 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.
- TheAceOfHearts 5y agoSomeone already commented that you can use `[...iterable]` to convert any iterable into an array, but if you're doing this just to call forEach on it then it's probably better to use for-of in order to avoid allocating an additional array: for (const item of iterable) { // do forEach stuff here }