Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to change specific object in array javascript

//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));

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

//Log object to console.
console.log("After update: ", myArray[objIndex])
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #change #specific #object #array #javascript
ADD COMMENT
Topic
Name
9+5 =