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

104
Views
How can I assign array values into object keys for other object?

So I have an array of objects [{}, {}, {}] and simple array [true, false, true]

Is there any way to make first array [{value: true}, {value: false}, {value: true}]?

I mapped over the object array

const a = [{a: 1}, {a: 2}, {a: 3}] const b = [true, false, true]  const c = a.map((item) => {...item, value: }) 

I don't understand how to assign the value from the second array.

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

0

Assuming your array of objects is called objects and your simple array is called array, then you can execute:

objects.forEach((o, i) => o['value'] = array[i])
about 4 years ago · Juan Pablo Isaza Report

0

You can map each element of the array to the object:

console.log([true, false, true].map(el => ({ value: el })));

or if you want to overwrite the elements in the first array

const arr = [{}, {}, {}];
const arr2 = [true, false, true];
arr.forEach((el, idx) => el.value = arr2[idx]);

console.log(arr);

about 4 years ago · Juan Pablo Isaza Report

0

An alternative way of accomplishing this is by using the unshift() method which will add the new elements to the beggining of the array.

var arr1 = [true, false, true], arr2 = [{}, {}, {}];
for(el of arr1){ arr2.unshift({'value': el}) }
console.log(arr2);

If you want to loose the last three empty objects in the array, slice them like so: arr2.slice(0,3)

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!