如何在数组对象中添加新属性

how may add a new properties in array objets

嗨,如何在数组对象中添加新属性

我想做的是添加一个新的 属性 以便我可以从 MDBTABLE

我有下一个例子

const instutions = [
  {
    name: 'Malasia',
    direction: 'Asia'
  },
  {
    name: 'New Francia',
    direction: 'paris'
  }
];

好吧,我想添加新的属性和结果它会是这样的

const instutions = [
  {
    name: 'Malasia',
    direction: 'Asia',
    buttonAdd: 'Hello world'
  },
  {
    name: 'New Francia',
    direction: 'paris',
    buttonAdd: 'Hello world'
  }
];

添加新属性,对象数组,你可以这样做

您可以使用 forEach 方法向数组的每个对象添加一个 属性

instutions.forEach(elem => elem.buttonAdd = "Hello world");

文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

您可以通过多种方式添加 属性。高阶函数map就是其中之一。请参阅示例。

const institutions = [{name: 'Malasia',direction: 'Asia'},{name: 'New Francia',direction: 'paris'}];
    
institutions.map(el => el.new_property = 'Hello')
    
console.log(institutions)