将键值对(来自数组的值)添加到数组中的特定对象

Posted

技术标签:

【中文标题】将键值对(来自数组的值)添加到数组中的特定对象【英文标题】:Adding key value pair (value from an array) to specific objects in an array 【发布时间】:2018-08-04 07:10:30 【问题描述】:

我有一个对象数组:

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

还有一个字符串数组。

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

我想为“角色”中的对象添加一个键值对,以便将正确的剑分配给正确的角色。需要将 Swords[0] 中的索引匹配到 charachrers[0] 中的值:

这是我希望角色的样子:

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

请帮忙。中土的既定事实取决于它。

【问题讨论】:

很多人已经回答了如何按照您的要求做,所以我不会做同样的事情。但是,与其将剑复制到角色对象中,将其保留为剑对象的单独集合并让剑的当前持有者仅存储剑的索引引用是否有益?剑会改变主人吗?您的方法看起来像是一次性的数据整理,而不是存储 2 个实际具有一对一关系的单独列表的理想方法。 我同意@Raith。如果您按照自己的方式进行操作,那么假设您 chachters 数组包含 100 个对象,swords 也将有 100 个字符串,WITH DUPLICATES (因为从逻辑上讲,你不会拥有 100 种不同类型的剑)。比如:["Sting", Glamdring", "Anduril", "Sting", "Anduril", ... ] 【参考方案1】:

您可以使用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);

【讨论】:

【参考方案2】:

您可以将array#mapspread syntax 一起使用。根据索引为角色添加一把剑。

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

【讨论】:

【参考方案3】:

使用#array.forEach 并为数组的每个对象添加带有剑数组值的额外键。

工作sn-p(这样,它会直接在原始数组中进行更改):

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

【讨论】:

【参考方案4】:

您可以创建一个函数来将字符串数组附加到对象数组中;

例如:

此函数将用于将字符串数组附加到对象数组中

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

【讨论】:

【参考方案5】:

让我试试。我对 .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" ]

【讨论】:

以上是关于将键值对(来自数组的值)添加到数组中的特定对象的主要内容,如果未能解决你的问题,请参考以下文章

javascript 将键值对添加到JavaScript对象

将键值对添加到 Javascript 对象中

PHP遍历键值对数组

javascript中数组取值的问题,如何取出键值对中的值?

如何将键值对添加到字典中? [复制]

怎样从对象中获取键值并保存在对象中