【问题标题】:Add key value (object) pair to all objects in array将键值(对象)对添加到数组中的所有对象
【发布时间】:2019-07-03 18:16:40
【问题描述】:

我的问题与Add key value pair to all objects in array有关

但是,当我尝试分配对象而不是字符串、int 等时,解决方案不起作用。

我尝试在 map 函数中创建新键,但它只适用于非对象变量。

这行得通

arrObjects.map(item => item.newKey = 'xxx')

这不是

var obj = { a: 'a', b: 'b', c: 'c' }
arrObjects.map(item => item.newKey = obj)

输出:

var arrOfObj = [{
  name: 'eve'
}, {
  name: 'john'
}, {
  name: 'jane'
}];
var obj = {
  a: 'a',
  b: 'b',
  c: 'c'
}
arrOfObj.map(item => item.newKey = obj);

console.log(arrOfObj);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【问题讨论】:

  • 什么是arrObjects?你能把剩下的代码贴出来吗?
  • 另外,你可能会澄清你的意思是什么不起作用。你在期待什么?发生了什么?
  • 在使用map 时不应该改变原始数组。改用:arrObjects.map(item => ({...item, newKey: {...obj}}));

标签: javascript arrays function object ecmascript-6


【解决方案1】:

您需要创建对象的副本。默认情况下,对象被指定为参考。 这里...用于创建浅拷贝

var arrOfObj = [{name: 'eve'}, {name: 'john'}, { name: 'jane'}];
var obj = { a: 'a', b: 'b', c: 'c'}

arrOfObj.forEach(item => (item.newKey = {...obj}));

console.log(arrOfObj);
.as-console-wrapper { max-height: 100% !important; top: 0; }

你可以看到一些用例here

【讨论】:

    【解决方案2】:

    您需要使用...(扩展运算符)

    var arrOfObj = [{ name: 'eve' }, { name: 'john' }, { name: 'jane' }];
    var obj = { a: 'a', b: 'b', c: 'c' };
    arrOfObj.forEach(item => item.newKey = {...obj});
    
    console.log(arrOfObj);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    或者,使用JSON:

    var arrOfObj = [{ name: 'eve' }, { name: 'john' }, { name: 'jane' }];
    var obj = { a: 'a', b: 'b', c: 'c' };
    arrOfObj.forEach(item => item.newKey = JSON.parse(JSON.stringify(obj)));
    
    console.log(arrOfObj);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案3】:

      您必须克隆该项目,您将同一对象添加到多个属性。

      请参阅下面的内容。

      var arrOfObj = [{
        name: 'eve'
      }, {
        name: 'john'
      }, {
        name: 'jane'
      }];
      var obj = {
        a: "a",
        b: "b",
        c: "c"
      }
      arrOfObj.map(item => 
      // clone the item 
      item.newKey = JSON.parse(JSON.stringify(obj))
      );
      
      console.log(arrOfObj);

      【讨论】:

        【解决方案4】:

        另一种方法是使用Object.assign()。请记住,对象是通过引用的值复制的,这是您的问题,而不是每个 newKey 的新对象,您对同一个对象有多个引用。

        var arrOfObj = [
          {name: 'eve'},
          {name: 'john'},
          {name: 'jane'}
        ];
        
        var obj = { a: 'a', b: 'b', c: 'c' };
        
        arrOfObj.map(item => item.newKey = Object.assign({}, obj));
        console.log(arrOfObj);
        
        // After some modification.
        arrOfObj[0].newKey.a = "XXX"
        console.log(arrOfObj);

        【讨论】:

        • 请注意,Object.create() 并不完全按照您所说的那样做。 Object.create() 创建一个新对象并将 prototype 设置为您传递给它的参数。这与创建对象的副本不同。就此而言,对 obj 进行更改仍会导致创建的对象发生可感知的更改,即使它们不共享引用。
        • @MadaraUchiha 你说的对,谢谢,我改成Object.assign()
        猜你喜欢
        • 1970-01-01
        • 2022-06-29
        • 2014-07-09
        • 2022-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-18
        • 2022-06-10
        相关资源
        最近更新 更多