What's the equivalent of a list comprehension like this one in ES2016 or greater?

前端 未结 4 1614
孤独总比滥情好
孤独总比滥情好 2020-12-19 05:16

Python 3.6:

[f\"Cat #{n}\" for n in range(5)]

gives

[\'Cat #0\', \'Cat #1\', \'Cat #2\', \'Cat #3\', \'Cat #4\']

Ne

4条回答
  •  再見小時候
    2020-12-19 05:59

    console.log( [...Array(5)].map((v, i) => `Cat #${i}`) )

    If it has to work in IE too :

    console.log( Array.apply(0, Array(5)).map(function(v, i) { return 'Cat #' + i; }) )

提交回复
热议问题