Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 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 Answer

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...