Wasteland Wasteland - 1 month ago 7
Javascript Question

ES6 - generate an array of numbers

Having googled for it I found two solutions:



var data = [...Array(10).keys()];
console.log(data);
var data1 = Array(8).fill().map((_, i) => i);
console.log(data1);





data1 displays [0, 1, ..., 7] however data just displays [[object Array Iterator]] how do I actually see the numbers.

I need it for some iterations over numbers (part of Euler project).

Previously I did a lot of Euler challenges in Python. Now I decided I'll revisit it and do as much as I can in JS (as much ES6 syntax as possible) to help me develop my js skills.

Answer

Here is a simple solution that works in codepen:

Array.from(Array(10).keys())

To be clear, Array.from() and Array.keys() require an ES6 polyfill in order to work in all browsers.