“Ubah Nama Properti Objek di Array JavaScript” Kode Jawaban

Ubah Nama Properti Objek di Array JavaScript

const columns = [
    { name: 'OrderNumber', title: 'Order Number' },
    { name: 'strawberry', title: 'Strawberry' },
    { name: 'vanilla', title: 'Vanilla' }
]

const options = columns.map(function(row) {

   // This function defines the "mapping behaviour". name and title 
   // data from each "row" from your columns array is mapped to a 
   // corresponding item in the new "options" array

   return { value : row.name, label : row.title }
})

/*
options will now contain this:
[
    { value: 'OrderNumber', label: 'Order Number' },
    { value: 'strawberry', label: 'Strawberry' },
    { value: 'vanilla', label: 'Vanilla' }
];
*/
Naughty Nightingale

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

Ubah Nama Properti Objek di Array JavaScript

const columns = [
    { name: 'OrderNumber', title: 'Order Number' },
    { name: 'strawberry', title: 'Strawberry' },
    { name: 'vanilla', title: 'Vanilla' }
];

const newColumns = columns.map( item => {
  const { name: value, ...rest } = item;
  return { value, ...rest }
 }
);

console.log( newColumns );
 Run code snippet
Naughty Nightingale

Jawaban yang mirip dengan “Ubah Nama Properti Objek di Array JavaScript”

Pertanyaan yang mirip dengan “Ubah Nama Properti Objek di Array JavaScript”

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

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya