2015-11-03 83 views
0

請幫我解決這個問題。JavaScript Array未對齊

腳本

<div id="data"></div> 
<script> 
var data = document.getElementById('data'); 
var str = '0.00,0.00,0.00,0.00,0.00,0.00,0.00,21.16,18.28,0.00,0.00,0.00,0.00'; 
var arr = str.split(','); 
var ar = new Array(); 
for(i=1; i<=arr.length; i++){ 
    ar.push(i+' ===== '+arr[i]+'<br>'); 
} 
data.innerHTML = ar; 
</script> 

結果

1 ===== 0.00 
,2 ===== 0.00 
,3 ===== 0.00 
,4 ===== 0.00 
,5 ===== 0.00 
,6 ===== 0.00 
,7 ===== 21.16 
,8 ===== 18.28 
,9 ===== 0.00 
,10 ===== 0.00 
,11 ===== 0.00 
,12 ===== 0.00 
,13 ===== undefined 

我想帶21.16和18.28〜8和9,其確切的值應該是。 爲什麼21.16落在7和18.28在8?它應該在8和9中。

+0

數組索引從0開始而不是1這種情況我相信每種編程語言。所以如果你數到8,它實際上是7,而9是8.等 – Sir

+0

@Dave:不是每種語言。 AWK中的索引例如1(0具有整個數組/列表的特殊含義)。還有像Lua和Fortran這樣的語言,可以從**任意數字**索引,因爲它們的數組可以用下限聲明。請參閱:https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(array) – slebetman

+0

感謝您的幫助<3 –

回答

2
for(i=0; i<arr.length; i++){ 
    ar.push((i+1)' ===== '+arr[i]+'<br>'); 
} 
+0

ar.push((i + 1)+'====='+ arr [i] +'
'); –