Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

javascript - 从1个数字创建一个数组(递增)?(Create an array from 1 number, (which increments)?)

How do I create an array, which increments up to a certain number?(如何创建一个递增到一定数量的数组?)

For example, I have a variable, with the value of 3:(例如,我有一个变量,其值为3:) const totalNumber = 3; Is it possible, to convert this to an array, but the array increments from every number, up to and including 3?(是否可以将其转换为数组,但数组从每个数字开始递增,一直到3,包括3?) For example, I would want the out put to be:(例如,我希望输出为:) [1,2,3] So if the value was 10 , output would be:(因此,如果值为10 ,则输出为:) [1,2,3,4,5,6,7,8,9,10]   ask by Reena Verma translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use loop(使用循环)

const totalNumber = 3; var arr = []; for(var i=1; i<=totalNumber; i++) { arr.push(i); } console.log(arr);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...