Answers for "js how to update property of array of object"

12

update an item in array of object

//Initailize array of objects.
let myArray = [
  {id: 0, name: "Jhon"},
  {id: 1, name: "Sara"},
  {id: 2, name: "Domnic"},
  {id: 3, name: "Bravo"}
],
    
//Find index of specific object using findIndex method.    
objIndex = myArray.findIndex((obj => obj.id == 1));

//Log object to Console.
console.log("Before update: ", myArray[objIndex])

//Update object's name property.
myArray[objIndex].name = "Laila"

//Log object to console again.
console.log("After update: ", myArray[objIndex])
Posted by: Guest on May-11-2020
1

update property in object in array javascript

var result = foo.map(el => el.bar == 1 ? {...el, baz: [11,22,33]} : el);
Posted by: Guest on November-25-2021
0

update property of object in array javascript

foo.forEach(function(obj) {
    if (obj.bar === 1) {
        obj.baz[0] = 11;
        obj.baz[1] = 22;
        obj.baz[2] = 33;
        // Or: `obj.baz = [11, 22, 33];`
    }
});
Posted by: Guest on December-14-2021

Code answers related to "js how to update property of array of object"

Code answers related to "Javascript"

Browse Popular Code Answers by Language