简体   繁体   中英

How to push a new element to all objects in an array?

Hello guys I am trying to push a new element to all the objects in an array but seem to not update.

Any idea what am I doing wrong in here?

So this is my object which contain the instalments array.

{ _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 }

What I want to do is to add offer to all instalments objects

The way I am trying to do this is as follow.

instalmentsGroup is the object above then I access instalments and map

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

Your issue is with await . There is no need for await as .map does not return a Promise , and so it is not asynchronous.

If you want to edit your object in-place you can use .forEach which would loop through each object in your instalments array and add the order property to it.

See example below:

 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); 


Alternatively, you could use .map() like you have to map each object to itself, with an additional offer property which you can get from the original object. Just make sure to remove await . This approach will create a new altered array (and won't modify the original object)

See example below:

 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); 

Another solution without mutating the object might be:

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

If you want updated array of instalments only, then assign an extra property through map method

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

Also there is no need of async/await as map does not return a promise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM