1

I wanted to add a key:value parameter to all the objects in an array from another array eg:

var arrOfObj = [{id: 001, date:'22/05/2020', Actor:'jane'},
                {id: 002, date:'02/03/2020', Actor:'alice'},
                {id: 003, date:'11/06/2020', Actor:'jean'},
                {id: 004, date:'20/01/2020', Actor:'yann'}];

var arrayScore = [44,2,3,5];

I want add for every objects a key:value parameter from arrayScore, like :

var arrOfObj = [{id: 001, date:'22/05/2020', Actor:'jane', score:44},
                {id: 002, date:'02/03/2020', Actor:'alice', score:2},
                {id: 003, date:'11/06/2020', Actor:'jean', score:3},
                {id: 004, date:'20/01/2020', Actor:'yann', score:5}];

I tried this code:

    var result = arrOfObj.map(function(el) {
                     var o = Object.assign({}, el);
                     o.score = arrayScore;
                     return o;
                     });
   console.log(result);

but arrOfObj add all values from arrayScore for every object!! How can I change this please??

Thank you for your HELP!

2
  • 2
    Are your id values really numbers, or are they strings? If they're numbers, I strongly recommend not using the legacy octal format to write them. If they're meant to be strings, put them in quotes. Commented Feb 15, 2021 at 11:14
  • arrOfObj.map((item, idx) => { item.score = arrayScore[idx]; return item; }) Commented Feb 15, 2021 at 11:15

2 Answers 2

1

You can use Array.map to create the new array including the user scores, I would also take note of TJCrowders's point about the Ids.

var arrOfObj = [{id: 1, date:'22/05/2020', Actor:'jane'},
                {id: 2, date:'02/03/2020', Actor:'alice'},
                {id: 3, date:'11/06/2020', Actor:'jean'},
                {id: 4, date:'20/01/2020', Actor:'yann'}];

var arrayScore = [44,2,3,5];

const result = arrOfObj.map((el, index) => ({...el, score: arrayScore[index] }));
console.log("Result with scores:", result);

Sign up to request clarification or add additional context in comments.

Comments

1

Since you do not need a new array of objects, but only need to add the properties to the objects in the array, you can use the array method forEach instead of map.

If we pass two parameters to the callback provided to forEach, the second parameter will receive the index of the array element we are iterating over. This allows us to assign the corresponding value from the arrayScore array.

This should work

arrOfObj.forEach((o, i) => {
    o.score = arrayScore[i];
});

Cheers!

2 Comments

Hi @camelCaseForTheWin, welcome to SO. Please consider adding some explanation of how your code works/why it works. Simply providing the code might not be enough for people to understand. Thanks
Sorry @GBra, I hope my edit improved the response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.