开发者

how to assign values to multidimensional arrays in javascript?

I开发者_运维问答 tried to do this with:

arr[i][j] = 'whatever';

but I get some kind of error "cannot convert to object..."


I'm going to guess that you haven't initialized a[i] when you try to treat it like an array. If you haven't initialized a[i] to be an array when you say a[i][j], then it will be undefined (or something else that isn't an array or object) and that doesn't know what [j] means, hence your "cannot convert to an object" error. You need something more like this:

var a = [ ];
for(var i = 0; i < 10; ++i) {
    a[i] = [ ];
    for(var j = 0; j < 10; ++j) {
        a[i][j] = 42; // a[i] is now an array so this works.
    }
}


Set Like

a[3]["fieldName"]="xxxx";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜