简体   繁体   English

JS:将对象从一个数组添加到另一个数组内的对象

[英]JS: Add objects form an array to objects inside another array

DISCLAIMER: Please dont mind my stupid object names, its just to learn the basics免责声明:请不要介意我愚蠢的 object 名称,它只是为了学习基础知识

I have an array of objects named "cars":我有一个名为“汽车”的对象数组:

const cars = [
  {
    name: "red",
    competes: true,
    category: 2,
    given: 400
  },
  {
   name: "blue",
   competes: false,
   category: 2,
    given: 0
  },
  {
    name: "green",
    competes: true,
     category: 2,
    given: 0
  }
]

And another array of objects named addOns:还有另一个名为 addOns 的对象数组:

const addOns = [
  {
    name: "hyperblast",
    upgrade: 100
  },
  {
    name: "catalyst",
    upgrade: 400
  }
]

Question问题

How can i add for example the addOn {name: "catalyst", upgrade: 400} to the car named "green"?例如,如何将 addOn {name: "catalyst", upgrade: 400} 添加到名为 "green" 的汽车中?
Ive tried with我试过了

cars[2].push(addOns[1]);

but it doesnt work.但它不起作用。 And all my google searches were without result (arrays of objects seem so important but I constantly fail to find anything regarding. Only about arrays or objects, never together)而且我所有的谷歌搜索都没有结果(对象数组似乎很重要,但我总是找不到任何相关信息。只有关于 arrays 或对象,从不在一起)

You could add the addOns as another field of the car object.您可以将插件添加为汽车 object 的另一个字段。 For your example: cars[2].extra = addOns[0];对于您的示例: cars[2].extra = addOns[0]; . . After typing this, if you try to console the cars[2] you will see:输入此内容后,如果您尝试控制汽车[2],您将看到:

{
  name: 'green',
  competes: true,
  category: 2,
  given: 0,
  extra: { name: 'hyperblast', upgrade: 100 }
}

Since you are working with vanilla Js, the cars[0].extra is going to work, but if you use TS in future, it would be better to define a field before adding it, or changing the structure of the script completely由于您使用的是 vanilla Js,所以cars[0].extra可以工作,但是如果您将来使用 TS,最好在添加之前定义一个字段,或者完全更改脚本的结构

As cars and addOns both have the property key 'name', I would change the second one to 'addOnName' or something of that sort.由于汽车和附加组件都具有属性键“名称”,因此我会将第二个更改为“附加名称”或类似的东西。 Otherwise, 'green' will just be overwritten to 'hyperblast' in the result.否则,结果中的“green”将被覆盖为“hyperblast”。

Being that you're trying to add multiple properties to an object, you should use a for...in loop.由于您正在尝试将多个属性添加到 object,因此您应该使用 for...in 循环。 As you can see below, I've cycled through the cars array with a regular for loop.正如您在下面看到的,我使用常规的 for 循环遍历了 cars 数组。 If the given car's name is 'green' I call the addProps function and pass it the given car object as an argument.如果给定汽车的名称是“green”,我调用 addProps function 并将给定汽车 object 作为参数传递给它。 Each prop in your chosen addOns object is then added on to the given car obj.然后将您选择的插件 object 中的每个道具添加到给定的汽车 obj 中。

const addProps = (obj) => {
  for (let prop in addOns[0]) {
    obj[prop] = addOns[0][prop];
  }
}

for (let i = 0; i < cars.length; i++) {
  if (cars[i].name === 'green') {
    addProps(cars[i]);
  }
}

console.log(cars);

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

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