js change value in array of objects code example

Example 1: 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])

Example 2: find and replace value in array of objects javascript

let arr = [
  {
    "enabled": true,
    "deviceID": "eI2K-6iUvVw:APA",
  },
  {
    "enabled": true,
    "deviceID": "e_Fhn7sWzXE:APA",
  },
  {
    "enabled": true,
    "deviceID": "e65K-6RRvVw:APA",
  },
];

const id = 'eI2K-6iUvVw:APA';

arr.find(v => v.deviceID === id).enabled = false;

console.log(arr);