向数组中特定对象添加键值对(值来自另一个数组)

3

我有一个对象数组:

chachters = [{name: "Frodo", race: "hobitt", age: 111}, 
             {name: "Gandalf", race: "human", age: 2019}],
             {name: "Aragorn", race: "elf", age: 40}];

以及一个字符串数组。

swords = ["Sting","Glamdring","Anduril"];

我希望将一个键值对添加到“characters”中的对象中,以便正确的剑被分配给正确的角色。索引匹配,因此需要将swords [0]添加到charachrers [0]中的值:

这是我想要的“characters”外观:

 chachters =[{name:"Frodo", race:"hobitt", age:111,sword:"Sting"}, 
             {name:"Gandalf",race:"human",age:2019,sword:"Glamdring"}],
             {name:"Aragorn",race:"elf",age:40,sword:"Anduril"}];

请帮忙。中土世界的命运取决于此。


1
很多人已经回答了如何做你所要求的事情,所以我不会重复。但是,与其将剑复制到字符对象中,保留它作为一个独立的剑对象集合,并让当前持有剑的人只存储他们的剑的索引引用,这样是否更有益呢?剑是否会改变主人?你的方法看起来像是一次性的数据整理,而不是存储实际上具有1对1关系的2个分离列表的理想方式。 - Raith
我同意@Raith的观点。如果你按照你现在的方式进行,那么假设你的chachters数组包含100个对象,swords也将有100个字符串,带有重复项_(因为你逻辑上不会有100种不同类型的剑)_。类似这样:["Sting", Glamdring", "Anduril", "Sting", "Anduril", ... ] - theAlexandrian
5个回答

3
你可以使用 mapObject.assign

var chachters = [{name: "Frodo", race: "hobitt", age: 111}, 
             {name: "Gandalf", race: "human", age: 2019},
             {name: "Aragorn", race: "elf", age: 40}],
    swords = ["Sting","Glamdring","Anduril"];

var result = chachters.map( (obj, i) => Object.assign({ sword: swords[i] }, obj) );
console.log(result);


3
你可以使用具有扩展语法的array#map。根据索引为角色添加一把剑。

const chachters = [{name: "Frodo", race: "hobitt", age: 111}, {name: "Gandalf", race: "human", age: 2019},       {name: "Aragorn", race: "elf", age: 40}],
      swords = ["Sting","Glamdring","Anduril"],
      result = chachters.map((o,i) => ({...o, sword: swords[i]}));
console.log(result);


2

使用 #array.forEach,对于数组中的每个对象,添加额外的键,并将值从swords数组中获取。

工作示例(这样,它将直接在原始数组中进行更改):

let chachters = [
      {name: "Frodo", race: "hobitt", age: 111}, 
      {name: "Gandalf", race: "human", age: 2019},
      {name: "Aragorn", race: "elf", age: 40}];

let swords = ["Sting","Glamdring","Anduril"];

chachters.forEach((el,i) => {
     el.sword = swords[i];
})

console.log('chachters = ', chachters);

如果chachters是一个状态数组,您要更新状态,请使用以下方式:

let chachters = [
      {name: "Frodo", race: "hobitt", age: 111}, 
      {name: "Gandalf", race: "human", age: 2019},
      {name: "Aragorn", race: "elf", age: 40}];

let swords = ["Sting","Glamdring","Anduril"];

let newchachters = chachters.map((el,i) => ({...el, sword: swords[i]}))

console.log('chachters = ', chachters);
console.log('newchachters = ', newchachters);


1
你可以创建一个函数,将字符串数组添加到对象数组中;
例如:
这个函数将用于将字符串数组附加到对象数组中。
function appendObjTo(swords, chachters ) {
        return Object.freeze(swords.concat(chachters ));
    } 

根据您所定义的内容:
  swords = ["Sting","Glamdring","Anduril"];
        const chachters = [{name: "Frodo", race: "hobitt", age: 111}, 
             {name: "Gandalf", race: "human", age: 2019},
             {name: "Aragorn", race: "elf", age: 40}];

        const newChachters = appendObjTo(swords, chachters);

1

让我来试试。我对 .map() 不是很熟悉 :P

var characters = [
    {name: "Frodo", race: "hobitt", age: 111}, 
    {name: "Gandalf", race: "human", age: 2019},
    {name: "Aragorn", race: "elf", age: 40}
];
var swords = ["Sting", "Glamdring", "Anduril"];

var charactersWithSwords = characters.map(function (character, index) {
    character.swords = swords[index];
    return character;
});

console.log(charactersWithSwords);

结果:

> Array [Object { name: "Frodo", race: "hobitt", age: 111, swords: "Sting" }, Object { name: "Gandalf", race: "human", age: 2019, swords: "Glamdring" }, Object { name: "Aragorn", race: "elf", age: 40, swords: "Anduril" }]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接