javascript - What's the equivalent of a list comprehension like this one in ES2016 or greater? -
python 3.6:
[f"cat #{n}" n in range(5)]
gives
['cat #0', 'cat #1', 'cat #2', 'cat #3', 'cat #4']
new javascript, what's equivalent in new ecmascript?
array comprehension in js proposed es2016, never made final release. firefox supported comprehensions time, support dropped in later versions.
you can use array#from close comprehension.
const result = array.from({ length: 5 }, (_, k) => `cat #${k}`); console.log(result);
Comments
Post a Comment