简体   繁体   English

如何动态地在JavaScript中的数组上的对象内部创建对象

[英]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 ? 我如何将seller1seller2动态添加到现有对象lista

I would recommend storing the sellers in an array and later use push : 我建议将卖方存储在数组中,然后再使用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: 正如上面SomoKRoceS回答的那样,您可以通过以下方式执行脚本:

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/ https://jsfiddle.net/9ar58teo/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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