“Perbarui Properti Objek di Array JavaScript” Kode Jawaban

Perbarui properti di objek di array javascript

var result = foo.map(el => el.bar == 1 ? {...el, baz: [11,22,33]} : el);
Mohamed Mahfouz

Perbarui properti objek array

//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])
 Run code snippet

Ubah Properti di Array Objek JavaScript

//change in array itself without need to another one 
arr.map(el =>{ el.bar == 1 && el.baz--} ); // don't forget {} in arrow function
Salsabeel woh woh

Perbarui Properti Objek di 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];`
    }
});
Quaint Quail

Jawaban yang mirip dengan “Perbarui Properti Objek di Array JavaScript”

Pertanyaan yang mirip dengan “Perbarui Properti Objek di Array JavaScript”

Lebih banyak jawaban terkait untuk “Perbarui Properti Objek di Array JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya