问题描述
为此,我找到了两个解决方案:
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显示[0,1,...,7],但是数据仅显示[[object Array Iterator]]我如何实际看到数字.
data1 displays [0, 1, ..., 7] however data just displays [[object Array Iterator]] how do I actually see the numbers.
我需要它来进行一些数字上的迭代(Euler项目的一部分).
I need it for some iterations over numbers (part of Euler project).
以前,我在Python中做了很多Euler挑战.现在,我决定重新访问它,并在JS中尽我所能(尽可能多地使用ES6语法)来帮助我发展自己的JS技能.
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.
推荐答案
这是在Codepen中工作的简单解决方案:
Here is a simple solution that works in codepen:
Array.from(Array(10).keys())
为清楚起见,Array.from()
和Array.keys()
需要ES6 polyfill才能在所有浏览器中工作.
To be clear, Array.from()
and Array.keys()
require an ES6 polyfill in order to work in all browsers.
这篇关于ES6-生成数字数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!