I wanted to create a multi dimensional object. Below is my sample code. I'm not very familiar in JavaScript.
Sample code
var test = {};
test[0] = {1:{a:1,b:2,c:3}};
if(true)
{
test[0] = {2: {c:1,b:2,a:3}};
}
console.log(test);
Expecting result
{
0: {
1:{a:1,b:2,c:3},
2:{c:1,b:2,a:3}
}
}
The second time you set test[0]
, you're overwriting it completely. To do exactly what you're looking for, you'd use:
test[0][2] = {c:1,b:2,a:3};
Depending on what you're doing though, you might consider an array []
or Set instead.
You can Try this :
var obj = {
0: {
1:{a:1,b:2,c:3},
2:{c:1,b:2,a:3}
}
} ;
// to access :
alert(obj[0][1].b);
// Or
alert(obj[0][1]["b"]);
Run and test it in here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.