Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript one liner range array

Tags:

javascript

Is there a nice one liner for a range array in javascript, equivalent to python's list(range(m, n))? ES6 permitted. Here's the best I've come up with so far:

[x for(x of (function*(){for(let i=0;i<100;i++) yield i})())]
like image 904
simonzack Avatar asked May 01 '26 15:05

simonzack


1 Answers

You can use Array.from and arrow functions for a better readability:

Array.from({length: 4}, (_, n) => n) // = [0, 1, 2, 3]
Array.from({length: 5}, (_, n) => n+6) // = [6, 7, 8, 9, 10]
Array.from({length: 6}, (_, n) => n-3) // = [-3, -2, -1, 0, 1, 2]
like image 82
Sebastien C. Avatar answered May 04 '26 05:05

Sebastien C.