admin 管理员组文章数量: 1086019
I have an array of objects:
chachters = [{name: "Frodo", race: "hobitt", age: 111},
{name: "Gandalf", race: "human", age: 2019}],
{name: "Aragorn", race: "elf", age: 40}];
and an array of strings.
swords = ["Sting","Glamdring","Anduril"];
I want to add a key-value pair to the objects in 'characters' so the correct sword is assigned to the correct character. The indexes match in that swords[0] needs to be added to the value in charachrers[0]:
Here's what I'd like characters to look like:
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"}];
Please help. The fait of middle earth depends on it.
I have an array of objects:
chachters = [{name: "Frodo", race: "hobitt", age: 111},
{name: "Gandalf", race: "human", age: 2019}],
{name: "Aragorn", race: "elf", age: 40}];
and an array of strings.
swords = ["Sting","Glamdring","Anduril"];
I want to add a key-value pair to the objects in 'characters' so the correct sword is assigned to the correct character. The indexes match in that swords[0] needs to be added to the value in charachrers[0]:
Here's what I'd like characters to look like:
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"}];
Please help. The fait of middle earth depends on it.
Share Improve this question asked Feb 24, 2018 at 9:16 bitcoder.iobitcoder.io 516 bronze badges 2- 1 Plenty of people have answered how to do what you asked so I won't do the same. But rather than copy the swords into the characters objects, might it be beneficial to retain it as a separate collection of sword objects and for the current holder of a sword to just store the index reference of their sword? Do swords ever change change owners? Your approach looks like a one-off data tidy-up, and not an ideal way to store 2 separate lists that actually have a 1-to-1 relationship. – Raith Commented Feb 24, 2018 at 9:53
-
I agree with @Raith. If you do it the way you are doing, then assuming you
chachters
array contains 100 objects, theswords
will also have 100 strings, WITH DUPLICATES (Because you logically won't have 100 different types of swords). Something like:["Sting", Glamdring", "Anduril", "Sting", "Anduril", ... ]
– theAlexandrian Commented Feb 24, 2018 at 10:26
5 Answers
Reset to default 3You can use map
and Object.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);
You can use array#map
with spread syntax
. Add a sword to a character based on the index.
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);
Use #array.forEach and for each object of array add extra key with the value from swords array.
Working snippet (This way, it will do the changes directly in the original array):
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);
If chachters
is a state array and you are updating the state then use this way:
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);
You can create a function to append the array of strings to the array of objects;
For example:
This function will be used to append the array of strings to the array of object
function appendObjTo(swords, chachters ) {
return Object.freeze(swords.concat(chachters ));
}
From what you defined:
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);
Allow me to try. I'm not so familiar with .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);
Result:
> 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" }]
本文标签: javascriptAdding key value pair (value from an array) to specific objects in an arrayStack Overflow
版权声明:本文标题:javascript - Adding key value pair (value from an array) to specific objects in an array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744076044a2529367.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论