比方說,我得到了它包含19.我想打一個陣列從它下面的數字使2位數字的數組在javascript
var positions = [ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19" ];
數的變量這是如何實現的JavaScript的?
比方說,我得到了它包含19.我想打一個陣列從它下面的數字使2位數字的數組在javascript
var positions = [ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19" ];
數的變量這是如何實現的JavaScript的?
喜歡的東西:
var number = 19;
var arr = [];
for (i = 0; i <= number ; i++) {
arr.push(i < 10 ? ("0" + i.toString()) : i.toString());
}
或更簡單:arr.push((i <10?'0':'')+ i) – HBP
或者:
var mynumber = 19,
myarr = String(Array(mynumber+1))
.split(',')
.map(function(el,i){return i<10 ? '0'+i : ''+i;});
對於zeropadding您可能需要使用:
function padLeft(nr,base,chr){
base = base || 10;
chr = chr || '0';
var len = (String(base).length - String(nr).length)+1;
return len > 0? Array(len).join(chr)+nr : nr;
}
// usage
padLeft(1); //=> '01'
padLeft(1,100); //=> '001'
padLeft(1,10000,'-'); //=> '----1'
你有沒有試過? –
我知道如果數字是例如9我可以使它與下面的行09:var totalplayers =(「0」+ totalplayers).slice(-2);'但我不能建立一個數組與所有數字從00到totalplayers – Alexandros
http://stackoverflow.com/questions/1267283/how-can-i-create-a-zerofilled-value-using-javascript – jbabey