2016-11-22 41 views
-1

我想要的值如下分配到一個數組位置:錯誤指定位置數組

//Javascript file 
var totales = new Array(); 

$('.total').each(function(){ 
    var id = $(this).attr("id");//value of id => "empresa_bundle_revision_capitulos_1_requisitos_1_articulos_1_total" 
    var arrayids = id.split("_"); 
    if(totales[arrayids[4]] == undefined){ 
      totales[arrayids[4]] = new Array(); 
    } 
} 

在的console.log我得到這個怎麼totales值:

[undefined, []] 

這應該返回類似於:

[[]] 

問題在哪裏?

+0

添加帶'.total'類的html。 –

+0

'arrayids [4]'yeilds是什麼? – candidJ

+0

arrayids [4] return「1」 –

回答

0

不確定你試圖實現的目標是在@deceze指向的索引1上設置一個條目。如果要分配新值,請使用index 0:

if(totales[arrayids[4]]==undefined){ 
    totales.push([]); 
    console.log(totales); 
    totales[arrayids[4]] = new Array(); 
    console.log(totales); 
    } 
+0

米問題是答案@deceze,但謝謝! :) –