簡體   English   中英

數組讀取,然后停止-錯誤:無法設置未定義的屬性“ 0”

[英]Array reads, then stops- error: Cannot set property '0' of undefined

我正在嘗試將csv字符串讀入2D數組,由於某種原因,當它到達第一個循環的第二個迭代時,它將停止。

這是小提琴和代碼:

$(document).ready(function(){
var lay = [[]];

    document.getElementById("result").innerHTML = value;
 bits = value.split(',');
 console.log(bits.length);
 elm = bits.length/4;
 count=0;
 console.log(bits[6]); //here it reads but won't assign to the array 

 for (i=0; i<=elm; i++)
 {lay[i]=[];
  console.log('i:'+i);
 for (j=0; j<=4; j++)
 {   console.log('j:' + j);
     console.log('count:' + count);;
     lay[i][j] == bits[count];
     count = count + 1;
     console.log('bit value:' + bits[count]);
 }   
 }  
console.log(lay[0][0]);
});

讀取並顯示下一個元素的值,但是當我嘗試將數據分配給數組時,會出錯。

固定碼

在第一個循環中添加了數組的重新聲明,這使錯誤消失了。

小提琴

謝謝!

我認為您的外行陣列出了點問題。 是否設置了此元素? 躺[i] [j]

我想我解決了

http://jsfiddle.net/hgm8r/3/

我做了兩個更改:

在循環外聲明“ lay”值

var lay = Array();

並在第一個循環開始時對其進行更新

lay[i] = Array();

現在它可以正常運行了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM