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

169
Views
How can update values in an array of objects?

how to get new array newSelect using map ,forEach

const selects = [false, true, true, true, false];
    const oldSelects = [
      { select: true, name: 'AA' },
      { select: true, name: 'AA' },
      { select: true, name: 'AA' },
      { select: true, name: 'AA' },
      { select: true, name: 'AA' },
    ];
    const newSelect = [
      { select: false, name: 'AA' },
      { select: true, name: 'AA' },
      { select: true, name: 'AA' },
      { select: true, name: 'AA' },
      { select: false, name: 'AA' },
    ];

oldSelects[0].select === selects[0]

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

0

Hey you can map the old array to a new one.

const selects = [false, true, true, true, false];
const oldSelects = [{
    select: true,
    name: 'AA'
  },
  {
    select: true,
    name: 'AA'
  },
  {
    select: true,
    name: 'AA'
  },
  {
    select: true,
    name: 'AA'
  },
  {
    select: true,
    name: 'AA'
  },
];

const newSelect = oldSelects.map((object, index) => {
  object['select'] = selects[index]
  return object;
})

console.log(newSelect)

about 4 years ago · Juan Pablo Isaza Report

0

@Inder answer is great, but a map looks more clean imo:

const selects = [false, true, true, true, false];
const oldSelects = [{
    select: true,
    name: "AA"
  },
  {
    select: true,
    name: "AA"
  },
  {
    select: true,
    name: "AA"
  },
  {
    select: true,
    name: "AA"
  },
  {
    select: true,
    name: "AA"
  }
];
const newSelect = oldSelects.map((el, i) => ({
  select: selects[i],
  name: el.name
}));

console.log(newSelect);

about 4 years ago · Juan Pablo Isaza Report

0

Using forEach

const selects = [false, true, true, true, false];
const oldSelects = [{
    select: true,
    name: 'AA'
  },
  {
    select: true,
    name: 'AA'
  },
  {
    select: true,
    name: 'AA'
  },
  {
    select: true,
    name: 'AA'
  },
  {
    select: true,
    name: 'AA'
  },
];

const newSelects = []

oldSelects.forEach((selectItem, index) => newSelects.push({...selectItem, select: selects[index]}))
console.log(newSelects)

Using map

const selects = [false, true, true, true, false];
const oldSelects = [{
    select: true,
    name: 'AA'
  },
  {
    select: true,
    name: 'AA'
  },
  {
    select: true,
    name: 'AA'
  },
  {
    select: true,
    name: 'AA'
  },
  {
    select: true,
    name: 'AA'
  },
];
const newSelects = oldSelects.map((selectItem, index) => ({...selectItem, select: selects[index]}))

console.log(newSelects)

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!