使用范围内的连续数字创建和填充数组的有效方法 [英] Efficient way to create and fill an array with consecutive numbers in range

查看:16
本文介绍了使用范围内的连续数字创建和填充数组的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我使用这个 JavaScript创建一个包含从 0 到 n 的 n 个数字的数组

for the moment i use this JavaScript to create an array with n numbers from 0 to n

// javascript populate array 
var n=1000;
var arr=[n];
for (i=n;i>=0;i--) {
arr[i]=i;
console.log(arr[i]);
} 

这是完成任务的快速/正确方法吗?

is it the fast/right way to do the task?

推荐答案

你可以像这样使用 Array.from 做声明式方法:

You can do declarative approach while using Array.from like this:

arr = Array.from({length: 20}, (e, i)=> i)
console.log(arr)

这篇关于使用范围内的连续数字创建和填充数组的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆