如何将新元素推入数组中的所有对象?

时间:2019-06-05 14:59:52

标签: javascript

大家好,我试图将一个新元素推入数组中的所有对象,但似乎没有更新。

你知道我在这里做什么错吗?

这是我的对象,其中包含instalments数组。

{ _id: 5cf7d4fcc9c5846a69b48d41,
  offer: 5cf7d4fcc9c5846a69b48d40,
  instalments:
   [ { paid: false,
       _id: 5cf7d4fcc9c5846a69b48d44,
       description: 'Some deacription about yhis instalment',
       instalmentAmount: 100000,
       dueTo: 2019-06-06T23:00:00.000Z },
     { paid: false,
       _id: 5cf7d4fcc9c5846a69b48d43,
       description: 'Some deacription about yhis instalment',
       instalmentAmount: 100000,
       dueTo: 2019-06-13T23:00:00.000Z },
     { paid: false,
       _id: 5cf7d4fcc9c5846a69b48d42,
       description: 'Some deacription about yhis instalment',
       instalmentAmount: 91152,
       dueTo: 2019-06-20T23:00:00.000Z } ],
  user: 5cf53a1a8481923f72939940,
  createdAt: 2019-06-05T14:43:08.706Z,
  updatedAt: 2019-06-05T14:43:08.706Z,
  __v: 0 }

我要做的是向所有offer对象添加instalments

我尝试执行此操作的方法如下。

instalmentsGroup是上方的对象,然后我访问instalments并映射

const instalments = await instalmentsGroup.instalments.map(
  instalment =>
    Object.assign({}, instalment, { offer: instalmentsGroup.offer })
);

3 个答案:

答案 0 :(得分:2)

您的问题出在await上。不需要await,因为.map不会返回Promise,因此它不是异步的。

如果要就地编辑对象,则可以使用.forEach,它会遍历instalments数组中的每个对象,并向其中添加order属性。

请参见以下示例:

const obj = {
  _id: "5cf7d4fcc9c5846a69b48d41",
  offer: "5cf7d4fcc9c5846a69b48d40",
  instalments: [{
      paid: false,
      _id: "5cf7d4fcc9c5846a69b48d44",
      description: 'Some deacription about yhis instalment',
      instalmentAmount: 100000,
      dueTo: "2019 - 06 - 06 T23: 00: 00.000 Z"
    },
    {
      paid: false,
      _id: "5cf7d4fcc9c5846a69b48d43",
      description: 'Some deacription about yhis instalment',
      instalmentAmount: 100000,
      dueTo: "2019 - 06 - 13 T23: 00: 00.000 Z"
    },
    {
      paid: false,
      _id: "5cf7d4fcc9c5846a69b48d42",
      description: 'Some deacription about yhis instalment',
      instalmentAmount: 91152,
      dueTo: "2019 - 06 - 20 T23: 00: 00.000 Z"
    }
  ],
  user: "5cf53a1a8481923f72939940",
  createdAt: "2019 - 06 - 05 T14: 43: 08.706 Z",
  updatedAt: "2019 - 06 - 05 T14: 43: 08.706 Z",
  __v: 0
}

const offer = obj.offer;
obj.instalments.forEach(instalment => {
  instalment.offer = offer;
});

console.log(obj.instalments);


或者,您可以使用.map(),就像必须将每个对象映射到自身一样,并带有一个附加的offer属性,该属性可以从原始对象获得。只需确保删除await。这种方法将创建一个新的更改后的数组(并且不会修改原始对象)

请参见以下示例:

const obj = {
  _id: "5cf7d4fcc9c5846a69b48d41",
  offer: "5cf7d4fcc9c5846a69b48d40",
  instalments: [{
      paid: false,
      _id: "5cf7d4fcc9c5846a69b48d44",
      description: 'Some deacription about yhis instalment',
      instalmentAmount: 100000,
      dueTo: "2019 - 06 - 06 T23: 00: 00.000 Z"
    },
    {
      paid: false,
      _id: "5cf7d4fcc9c5846a69b48d43",
      description: 'Some deacription about yhis instalment',
      instalmentAmount: 100000,
      dueTo: "2019 - 06 - 13 T23: 00: 00.000 Z"
    },
    {
      paid: false,
      _id: "5cf7d4fcc9c5846a69b48d42",
      description: 'Some deacription about yhis instalment',
      instalmentAmount: 91152,
      dueTo: "2019 - 06 - 20 T23: 00: 00.000 Z"
    }
  ],
  user: "5cf53a1a8481923f72939940",
  createdAt: "2019 - 06 - 05 T14: 43: 08.706 Z",
  updatedAt: "2019 - 06 - 05 T14: 43: 08.706 Z",
  __v: 0
}

const offer = obj.offer;
const instalments = obj.instalments.map(instalment => ({...instalment, offer}));

console.log(obj.instalments);

答案 1 :(得分:1)

不改变对象的另一种解决方案可能是:

function addOfferToInstalments(obj) {
  let output = { ...obj };
  const offer = output.offer;
  output.instalments = obj.instalments.map(instalment => ({ ...instalment, offer }));
  return output;
}

答案 2 :(得分:0)

如果您只想更新分期付款的数组,请通过map方法分配一个额外的属性

const updatedInstallments = instalmentsGroup.instalments.map(obj => {
    obj.offer = instalmentsGroup.offer;
    return obj;
})

而且async/await不需要返回承诺,因此不需要map