Wasteland Wasteland - 1 year ago 107
Javascript Question

ES6 - generate an array of numbers

Having googled for it I found two solutions:

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

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 Source

Here is a simple solution that works in codepen:


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download