0

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 })
);
2
  • loop through and then for each you add this ?- Commented Jun 5, 2019 at 15:03
  • Why you use await? Without await it works as you expected. Commented Jun 5, 2019 at 15:06

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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.

2 Comments

Markus had error in his code with await. The rest of code is good.
Yes, it's of no use there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.