简体   繁体   中英

How can I dynammicaly create an object inside a object, on a array in JavaScript

How do I dynamically create an object inside another object?

I got this code:

var lista = [{
        "product": "Dipers",
        "quantity": 2
    },
    {
        "product": "Beear",
        "quantity": 6
    },
    {
        "product": "Bread",
        "quantity": 10
    }
];

But I need to add new objects so it'll look something like this:

var lista = [{
        "product": "Dipers",
        "quantity": 2

        seller1 = {
        "name": "B&J",
        "adress": "that street"
        }
        seller2 = {
        "name": "B&J",
        "adress": "that street"
        }

    },
    {
        "product": "Beear",
        "quantity": 6
    },
    {
        "product": "Bread",
        "quantity": 10
    }
];

How would I add seller1 and seller2 dynamically to the existing object lista ?

I would recommend storing the sellers in an array and later use push :

var lista = [{
        "product": "Dipers",
        "quantity": 2

        sellers:[{
        "name": "B&J",
        "adress": "that street"
        },
        {
        "name": "B&J",
        "adress": "that street"
        }]

    },
    {
        "product": "Beear",
        "quantity": 6
    },
    {
        "product": "Bread",
        "quantity": 10
    }
];

Then, in your code, dynamically add a seller:

lista[desiredindex].sellers.push({name:"new",address:"new"});

You have a syntax error in your code, but as I understand you need this

 const seller1Name = “seller1”;
 const seller2Name = “seller2”;
 const lista = [{
    "product": "Dipers",
    "quantity": 2
   },
   {
    "product": "Beear",
    "quantity": 6
   },
   {
    "product": "Bread",
    "quantity": 10
   }
 ];
 lista[0][seller1Name] = {
   "name": "B&J",
   "adress": "that street"
 }

 lista[0][seller2Name] = {
     "name": "B&J",
     "adress": "that street"
 }

Just assign the new object properties that you wrote/retrieved to a new variable and then simply use dot notation to retrieve the new properties and assign it to your object.

Check and run the following Code Snippet for a practical example of what I described above:

 var lista = [{ "product": "Dipers", "quantity": 2 }, { "product": "Beear", "quantity": 6 }, { "product": "Bread", "quantity": 10 } ]; let newObj = { "seller1": { "name": "B&J", "adress": "that street" }, "seller2": { "name": "B&J", "adress": "that street" } } lista[0].seller1 = newObj.seller1; lista[0].seller2 = newObj.seller2; console.log(lista); 


NB An object property uses the colon : and not the equal sign = .

do like this..

var lista = [{
        "product": "Dipers",
        "quantity": 2
    },
    {
        "product": "Beear",
        "quantity": 6
    },
    {
        "product": "Bread",
        "quantity": 10
    }
];
///  try this


lista[0]['seller1']={
        "name": "B&J",
        "adress": "that street"
        };
lista[0]['seller2']={
        "name": "B&J",
        "adress": "that street"
        };

As was answered above by SomoKRoceS, you can do your script this way:

var lista = [{
        "product": "Dipers",
        "quantity": 2,
        "sellers": []
    },
    {
        "product": "Beear",
        "quantity": 6,
        "sellers": []
    },
    {
        "product": "Bread",
        "quantity": 10,
        "sellers": []
    }
];

var seller1 = {
    "name": "B&J",
  "adress": "that street"
}

var seller2 = {
    "name": "Test",
  "adress": "that street"
}

lista[0].sellers.push(seller1);
lista[1].sellers.push(seller2);
lista[2].sellers.push(seller1);
lista[2].sellers.push(seller2);

console.log(lista);

https://jsfiddle.net/9ar58teo/

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM