Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change value for specific objects in array

How can I change the status property to false of each entry with let’s say group "B", without changing the others?
Either by mutating the array or returning a new one

const personnel = [
  { id: 0, name: "Audrey Holcomb", group: "A", status: true },
  { id: 1, name: "Gwendolyn Heath", group: "B", status: true },
  { id: 2, name: "Rivas Mclean", group: "C", status: true },
  { id: 3, name: "Valeria Schneider", group: "A", status: true },
  { id: 4, name: "Leticia Keith", group: "C", status: true },
  { id: 5, name: "Nadine Woodward", group: "B", status: true },
  { id: 6, name: "Shaffer Oliver", group: "A", status: true },
  { id: 7, name: "Meghan Norman", group: "B", status: true },
  { id: 8, name: "Kyle Smith", group: "C", status: true },
]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Loop through array, check if group is B and then change status to false

for(let i = 0; i < personnel.length; i++) {
    if(personnel[i]['group'] == 'B') {
    personnel[i]['status'] = false;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading