“Ubah Nama Kunci Dalam Array Objek JavaScript” Kode Jawaban

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

Renemane Object Key JS

obj = { name: 'Bobo' } //key: value
obj.newName = obj.name // on object create new key name. Assign old value to this
delete obj.name //delete object with old key name
Amused Aardvark

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 Nama Kunci Dalam Array Objek JavaScript”

Pertanyaan yang mirip dengan “Ubah Nama Kunci Dalam Array Objek JavaScript”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya