1

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.

1
  • 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) Commented Jun 2, 2017 at 8:02

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

Comments

0

Try iterating over the dictionary with

for(var key in vm.classificationNames) {
   var entry = vm.classificationNames[entry];
   /*....*/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.