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
219 views
in Technique[技术] by (71.8m points)

javascript - 在JavaScript中使用2位数字制作数组(Making an array with 2 digit numbers in javascript)

Let's say that i got a variable which it contains the number 19. I want to make an array from it with the following numbers(假设我有一个包含数字19的变量。我想用以下数字从中构成一个数组)

var positions = [ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19" ];

How is this possible in javascript?(在javascript中怎么可能?)

  ask by Alexandros translate from so

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

1 Answer

0 votes
by (71.8m points)

Something like :(就像是 :)

var number = 19;
var arr = [];
for ( i = 0; i <= number ; i++ ) {
   arr.push(i < 10 ? ("0" + i.toString()) : i.toString());
}

demo : http://jsfiddle.net/Kfnnr/1/(演示: http : //jsfiddle.net/Kfnnr/1/)


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

...