0

Suppose I have an array of object:

const purchaseDetails = [
  {
      amount: '4567',
      purchaseOrder: 'B234',
      purchaseDate: '10/12/2020',
      someRandom1: '123',
      someRandom2: '345'
   },
   {
      amount: '4567',
      purchaseOrder: 'B234',
      purchaseDate: '10/12/2020',
      someRandom1: '678',
      someRandom2: '987'
    }
]

and another object as:

const toReplace = {
      amount: '1211',
      purchaseOrder: 'A123',
      purchaseDate: '12/30/2020',
      vat: '123',
 }

I want to replace value of purchaseDetails with toReplace value

O/p as:

const purchaseDetails = [
    {
         amount: '1211',
         purchaseOrder: 'A123',
         purchaseDate: '12/30/2020',
         vat: '123',
         someRandom1: '123',
         someRandom2: '345'
     },
     {
         amount: '1211',
         purchaseOrder: 'A123',
         purchaseDate: '12/30/2020',
         vat: '123',
         someRandom1: '678',
         someRandom2: '987'
      }
  ]

For this i tried as:

purchaseDetails.forEach(element => {
  element.amount=toReplace.amount,element.purchaseOrder=toReplace.purchaseOrder,
   element.purchaseDate=toReplace.purchaseDate,element.tax=toReplace.tax
});

But it doesnot replace the value...i tried with single value as well but it's not working.

As per provided solution it was assignment issue.

But the second solution provided, when I try:

purchaseDetails.forEach(element => {
        element = {...element, ...avacado}
        console.log(element)
});

On console.log(element), it shows element with value changed but console.log(purchaseDetails) value doesnot change.

4
  • 1
    == is used for comparisons. Use = for assignments. You should be able to do element = {...element, ...toReplace}. Commented Mar 10, 2021 at 11:47
  • Thank you..= assignment was the issue but your second solution looks much better but while I'm trying as below it doesnot work. Could you please guide? purchaseDetails.forEach(element => { element = {...element, ...toReplace} }); But it doesn't work.....is there something wrong that I am doing? Commented Mar 10, 2021 at 11:55
  • 1
    Yeah, sorry, that won't work here. However you can do Object.assign(element, toReplace); instead. Commented Mar 10, 2021 at 12:04
  • Thank you so much for the help....could you please provide it in the answer section so I could accept it. Commented Mar 10, 2021 at 12:07

1 Answer 1

1

If you instead declare purchaseDetails via let (thus allowing reassigning the result of map call later), you can use Array.prototype.map:

let purchaseDetails = [{
    amount: '4567',
    purchaseOrder: 'B234',
    purchaseDate: '10/12/2020',
    someRandom1: '123',
    someRandom2: '345'
  },
  {
    amount: '4567',
    purchaseOrder: 'B234',
    purchaseDate: '10/12/2020',
    someRandom1: '678',
    someRandom2: '987'
  }
]

const toReplace = {
  amount: '1211',
  purchaseOrder: 'A123',
  purchaseDate: '12/30/2020',
  vat: '123',
}

purchaseDetails = purchaseDetails.map(obj => ({ ...obj, ...toReplace }));

console.log(purchaseDetails);

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

Comments

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.