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

227
Views
How to change value of object which is inside an array

I have an array that contains a set of objects And in these objects I want to change the value of a property :

var array=[{a:1, b:false}, {a:2, b:true}, {a:3, b:false}]

I want the propriety b to be true everywhere

var array=[{a:1, b:true}, {a:2, b:true}, {a:3, b:true}]

How do I do this?

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

0

Just use a loop with map:

const newArr = array.map((item) => ({ ...item, b: true }));
about 4 years ago · Juan Pablo Isaza Report

0

you can use two possible approaches:

  1. you can use forEach as follow:
yourArray.forEach(item=>item.b = true);
  1. or if you want to build new array use map as follow:
const updatedArray = temp.map(item=>{...item, b:true});
about 4 years ago · Juan Pablo Isaza Report

0

There are 2 ways you can achieve the result:

  1. Using forEach:

var array=[{a:1, b:false}, {a:2, b:true}, {a:3, b:false}]
array.forEach((obj) => {
   return obj.b = true
})

console.log(array)

  1. Using map:

var array=[{a:1, b:false}, {a:2, b:true}, {a:3, b:false}];

const result = array.map((item) => ({ ...item, b: true }));

console.log(result);

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!