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})())]
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})())]
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]
You can just do this
var arr = []; while(arr[arr.length-1] !== end) arr.push(start++); // one-liner
Making the above to a function will result in
function createRange(start, end) {
var arr = []; while(arr[arr.length-1] !== end) arr.push(start++); // one-liner
return arr;
}