我是javascript的新手。當我使用對象和嵌套循環時。 plunker可Javascript基於嵌套for循環與對象
var a = [{b:[{c:null}]}]
for(var x= 0 ; x<10;x++){
for(var y= 0 ; y<10;y++){
console.log(a);
a[x].b[y].c = y;
console.log(a);
}
}
我得到錯誤,如TypeError: Cannot set property 'c' of undefined
可有人請解釋爲什麼它是這樣工作。我一直在尋找有這樣
a[0].b[0].c = 1;
a[0].b[1].c = 2;......
a[1].b[0].c = 1;....
a[9].b[9].c = 9;
你想做些什麼?你的預期產出是多少? – C2486
'a [9] .b [9] .c = 9;'因爲var a = [{b:[{c:null}}}]''不能工作' –
如果我需要,我該如何實現? ? – santhosh