I am trying to create a multidimentional arrya in javascript where I can add items in the following fashion:
var foo = {
"Internal": {
"0":
{
"pic_id":"15011",
"description":"Picture of a cpu",
"localion":"img.cloudimages.us/2012/06/02/mycpu.png",
"type":"png"
},
"1":{
"pic_id":"15011",
"description":"Picture of a cpu",
"localion":"img.cloudimages.us/2012/06/02/mycpu.png",
"type":"png"
}
},
"External":
{
"0":
{
"pic_id":"15014",
"description":"Picture of a cpu",
"localion":"img.cloudimages.us/2012/06/02/mycpu.png",
"type":"png"
}
}
};
but I don't know how to get my value into the main category. I got the following code
vm.classificationNames = [,];
for (var i = 0; i < vm.classificationNames.length; i++) {
vm.allGroupsInClassifications.push(vm.classificationNames[i]);
}
for (var i = 0; i < data.length; i++) {
var item = data[i];
if (item.classification != null) {
} else if (item.classification == null) {
vm.classificationNames['Internal'][item];
}
}
console.log(vm.classificationNames);
I also tried to use the following without any luck:
vm.classificationNames['Internal'].push(item);
Does anyone know what I'd be doing wrong? thanks for the help in advance.
asked Jun 2, 2017 at 7:58
Rodney Wormsbecher
9274 gold badges19 silver badges43 bronze badges
-
Also you say it's an array, but the object you show makes it seem that the "0" and "1" aren't array elements ( { } for objects vs [ ] for arrays)Peter-Paul– Peter-Paul2017年06月02日 08:02:41 +00:00Commented Jun 2, 2017 at 8:02
2 Answers 2
Thats because its an object, not an array.
Change your inner object to an array and push will work
var foo = {
"Internal": [ // <--- Note the square braces
{
"pic_id": "15011",
"description": "Picture of a cpu",
"localion": "img.cloudimages.us/2012/06/02/mycpu.png",
"type": "png"
},
{
"pic_id": "15011",
"description": "Picture of a cpu",
"localion": "img.cloudimages.us/2012/06/02/mycpu.png",
"type": "png"
}
], // <--- Note the square braces
"External": [ // <--- Note the square braces
{
"pic_id": "15014",
"description": "Picture of a cpu",
"localion": "img.cloudimages.us/2012/06/02/mycpu.png",
"type": "png"
}
] // <--- Note the square braces
};
foo['Internal'].push(item);
answered Jun 2, 2017 at 8:02
Coloured Panda
3,5273 gold badges23 silver badges33 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Try iterating over the dictionary with
for(var key in vm.classificationNames) {
var entry = vm.classificationNames[entry];
/*....*/
answered Jun 2, 2017 at 8:02
rainerhahnekamp
1,22413 silver badges27 bronze badges
Comments
lang-js