简体   繁体   English

JavaScript 将新对象附加到对象数组

[英]JavaScript Append a new object to an array of objects

Hi trying to add a new object to the list of cars for particular user, below is my array of object called inventory.嗨,尝试将新对象添加到特定用户的汽车列表中,下面是我的名为库存的对象数组。 Ive used a find to get the user id which return.我使用 find 来获取返回的用户 ID。

I want to add extra objects to the cars property ie {model: "Porsche",year: "2009"} to the user array when the user id = 1当用户 id = 1 时,我想向汽车属性添加额外的对象,即 {model: "Porsche",year: "2009"} 到用户数组

Is there a cleaner way of doing this without using push有没有更清洁的方法来做到这一点而不使用推送

 const inventory = [ { id: 1, name: "Paul", cars: [ { model: "Ford", year: "1995", }, { model: "BMW", year: "2010", }, ], }, { id: 2, name: "Simon", cars: [ { model: "Vauxhall", year: "2022", }, { model: "VW", year: "2001", }, ], }, ]; const found = inventory.find(element => element.id == 1); //console.log(found) const addNewObject = found.cars.concat({model: "Porsche",year: "2009"}) console.log(addNewObject)

If you want to modify the array in-place, use Array.push .如果要就地修改数组,请使用Array.push Otherwise spread operator is the way to go:否则传播运算符是要走的路:

const newInventory = [...inventory, {model: "Porsche",year: "2009"}];

You can use the spread operator:您可以使用扩展运算符:

const addNewObject = [...found.cars, {model: "Porsche",year: "2009"}];

This will give you the same result as your code.这将为您提供与您的代码相同的结果。

In case you want to know how to update the inventory in an immutable style (is this the reason why you don't like push ?), you can use map :如果您想知道如何以不可变样式更新inventory (这就是您不喜欢push的原因吗?),您可以使用map

const updatedInventory = inventory.map(item => 
 item.id === 1
   ? {...item, cars: [...item.cars, {model: "Porsche",year: "2009"}]}
   : item
);

You can use Array.prototype.map and update the item that has an id of 1 .您可以使用Array.prototype.map并更新id1的项目。

 const inventory = [ { id: 1, name: "Paul", cars: [ { model: "Ford", year: "1995" }, { model: "BMW", year: "2010" }, ], }, { id: 2, name: "Simon", cars: [ { model: "Vauxhall", year: "2022" }, { model: "VW", year: "2001" }, ], }, ]; const updatedInventory = inventory.map((item) => item.id === 1 ? { ...item, cars: item.cars.concat({ model: "Porsche", year: "2009" }) } : item ); console.log(updatedInventory);

If you want don't want to create a new array, then you can use Array.prototype.forEach instead of map .如果您不想创建新数组,则可以使用Array.prototype.forEach代替map

 const inventory = [ { id: 1, name: "Paul", cars: [ { model: "Ford", year: "1995" }, { model: "BMW", year: "2010" }, ], }, { id: 2, name: "Simon", cars: [ { model: "Vauxhall", year: "2022" }, { model: "VW", year: "2001" }, ], }, ]; inventory.forEach((item) => { if (item.id === 1) { item.cars.push({ model: "Porsche", year: "2009" }); } }); console.log(inventory);

const inventory = [
  {
    id: 1,
    name: "Paul",
    cars: [
      {
        model: "Ford",
        year: "1995",
      },
      {
        model: "BMW",
        year: "2010",
      },
    ],
  },
  {
    id: 2,
    name: "Simon",
    cars: [
      {
        model: "Vauxhall",
        year: "2022",
      },
      {
        model: "VW",
        year: "2001",
      },
    ],
  },
];

const addPropertyToCars=(id,newKeyValues)=>
inventory.map(item=>
item.id!==id ? item :{...item, cars:[...item.cars,newKeyValues]}
)


const newKeyValues={model: "Porsche",year: "2009"}

const newInventory=addPropertyToCars(1,newKeyValues)

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

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