Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
How to properly append array of element to array of object

I have these array and array of objects in my React code:

var A = [
  {id: 1, name: "han"},
  {id: 2, name: "mohd"},
]

var B = [100, 200];

and I want to append B to A, so that the output becomes something like this:

var A = [
  {id: 1, name: "han", score: "100"},
  {id: 2, name: "mohd", score: "200},
]

I tried doing it with the code below:

var newArray = [];

for (let k = 0; k < A.length; k++) {
        newArray = B.map(score => ({...A[k], score}));
}

return newArray;

However, the code above returns this output when console logged:

newArray = [
  {id: 2, name: "mohd", score: "100"},
  {id: 2, name: "mohd", score: "200},
]

It only appends the elements to the latest object, instead of to all objects. Anyone knows what is wrong with my code? Thank you!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

If you're going to use map you don't also need a loop since map will iterate over the array of objects, and return new array with new objects containing the score property.

Use the second parameter of the callback - the index - to identify which element of b we should be adding as the score.

const a = [
  {id: 1, name: 'han'},
  {id: 2, name: 'mohd'},
];

const b = [100, 200];

// `map` over `a` making sure we get the object
// in each iteration as well as its index
const out = a.map((obj, i) => {

  // Create a new object adding in the
  // new `score` property using the element in `b`
  // that matches the index
  return { ...obj, score: b[i] };

});

console.log(out);

about 4 years ago · Juan Pablo Isaza Report

0

Try something like this

const C = A.map((value, idx) =>({
   ...value,
   score: B[idx],
 }));

console.log(C)
about 4 years ago · Juan Pablo Isaza Report

0

If your both array lenght Will same then use this method

 var A = [
      {id: 1, name: "han"},
      {id: 2, name: "mohd"},
    ]
    
    var B = [100, 200];
    console.log(A.map((x,i)=>
        {return {
            ...x,
        score:B[i]
        }
        }
                     ))

If not sure About use this eg..

var A = [
  {id: 1, name: "han"},
  {id: 2, name: "mohd"},
   {id: 2, name: "mohd"},
]

var B = [100, 200];
console.log(A.map((x,i)=>
    {return {
        ...x,
    score:!B[i]?0:B[i]
    }
    }
                 ))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!