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

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

如何在另一个对象中动态创建一个对象?

我得到了这个代码:

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

但我需要添加新对象,所以它看起来像这样:

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
    }
];

如何将 seller1seller2 动态添加到现有对象 lista

我建议将卖家存储在一个数组中,稍后使用 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
    }
];

然后,在您的代码中,动态添加卖家:

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

您的代码中存在语法错误,但据我了解您需要这个

 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"
 }

只需将您 wrote/retrieved 的新对象属性分配给一个新变量,然后只需使用 点符号 即可检索新属性并将其分配给您的对象。

检查并 运行 以下 代码段 作为我上面描述的实际示例:

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);


N.B. 对象 属性 使用冒号 : 而不是等号 =.

喜欢这样..

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"
        };

正如上面 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/