Create array inside the JSON object

0

I am creating an array in JSON, and would like to know how to create another array inside the object.

var objeto;
objeto = {
    "item" : [
     {
        "id" : delDiv,
        "nome" : nomeItem.value,
        "cod" : codItem.value
      }
     ]
}

I would like to create a new item with new information without overwriting the information already placed.

    
asked by anonymous 26.09.2018 / 15:21

2 answers

1

Use the push array to add a new item within this attribute, an example follows:

var novo_item = {
    "id" : delDiv2,
    "nome" : nomeItem2.value,
    "cod" : codItem2.value
};

objeto.item.push(novo_item);

or

objeto.item.push({
    "id" : delDiv2,
    "nome" : nomeItem2.value,
    "cod" : codItem2.value
});
    
26.09.2018 / 15:34
0

You've tried, for example, this:

       objeto = {
            "item" : [
                {
                    "id" : delDiv,
                    "nome" : nomeItem.value,
                    "cod" : codItem.value
                },
                {
                    "id" : delDiv2,
                    "nome" : nomeItem2.value,
                    "cod" : codItem2.value
                },
                {
                    "id" : delDiv3,
                    "nome" : nomeItem3.value,
                    "cod" : codItem3.value
                }
            ],
            "array2" : [
               //objetos do outro array
            ]
       }

If you want to loop and insert multiple "items" in the array item , you can objeto.item.push(novo_item); where novo_item is the object to insert into the array.

    
26.09.2018 / 15:27