Full-time web dev; JS lover since 2002; CSS fanatic. #CSSIsAwesome
I try to stay up with new web platform features. Web feature you don't understand? Tell me! I'll write an article!
He/him
Another handy use is to convert iterable objects to Arrays when you need the Array methods:
/* Filtering a Set */constfruit=newSet(['apple','banana','avocado','watermelon'])// No no no!fruit.filter(f=>f.startsWith('a'))// TypeError: fruit.filter is not a function// Instead...[...fruit].filter(f=>f.startsWith('a'))// ["apple", "avocado"]/* Mapping over a generator's output */// counts from low to high-1function*range(low,high){for(leti=low;i<high;i++){yieldi}}// No no no!range(1,10).map(n=>n**2)// TypeError: range(...).map is not a function// Instead...[...range(1,10)].map(n=>n**2)// [1, 4, 9, 16, 25, 36, 49, 64, 81]
Just a thought I had: I sort of wish that your error example worked like Object.entries, and in fact that Objects were iterable as entries the way that Maps are. I wish I could do stuff like this (but to be clear, this does NOT currently work):
const o = {'a': 10, 'b': 20}
[...o] // [["a", 10], ["b", 20]]
for (const entry of o) console.log(entry)
// ["a", 10]
// ["b", 20]
Sigh... but I dream...
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Great summary! Two extra thoughts:
Object.entries
, and in fact that Objects were iterable as entries the way that Maps are. I wish I could do stuff like this (but to be clear, this does NOT currently work):Sigh... but I dream...