“Ubah Nilai Kunci dalam Array Objek JavaScript” Kode Jawaban

Perbarui item dalam array objek

//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])
Upset Unicorn

Ubah Nilai Kunci dalam Array Objek JavaScript

const users = [
  { name: "Alex", age: 25 },
  { name: "John", age: 32 },
];

const newUsers = users.map((user) => ({
  ...user,
  age: user.age + 5, // just for example
}));

// newUsers = [
// {name:"Alex" , age:30},
// {name:"John , age:37}
// ]
Disgusted Dragonfly

Ubah kunci dalam array objek JavaScript

let arrayObj = [{key1:'value1', key2:'value2'},{key1:'value1', key2:'value2'}];

arrayObj = arrayObj.map(item => {
      return {
        stroke: item.key1,
        key2: item.key2
      };
    });
Disgusted Dragonfly

Ubah Nama Kunci Dalam Array Objek JavaScript

const arrayOfObj = [{
  key1: 'value1',
  key2: 'value2'
}, {
  key1: 'value1',
  key2: 'value2'
}];
const newArrayOfObj = arrayOfObj.map(({
  key1: stroke,
  ...rest
}) => ({
  stroke,
  ...rest
}));

console.log(newArrayOfObj);
Hungry Horse

Jawaban yang mirip dengan “Ubah Nilai Kunci dalam Array Objek JavaScript”

Pertanyaan yang mirip dengan “Ubah Nilai Kunci dalam Array Objek JavaScript”

Lebih banyak jawaban terkait untuk “Ubah Nilai Kunci dalam Array Objek JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya