score:2

Accepted answer

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

score:1

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

score:1

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

score:3

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

score:3

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


Related Query

More Query from same tag