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