Auto repeated objects in Array after pushing

hello, i have a question about pushing new attributes to an existing data, i tried many methods, but i cannot find the answer yet about it, i push an attribute to an existing data 1 time, but it automatically repeats itself

calculatedeliverycharges() {

this.cart.forEach(element => {
  this.temp=element
  this.http.get("http://abc,com/api/shippings/calculate/" + element.zipcode + "/" + this.receivercode + "/" + element.quantity).subscribe(res => {

let temp = element

  temp['shippingcost'] = res['price'] 

  this.cart.push(element);

  console.log('what is updated cart', this.cart);

  return this.cart;

  })

  console.log('calculate delivery charges', this.cart);
});

}

i get the following result,

can anyone enlighten me where have i missed out or went wrong?

why i push 1 time, but it repeated itself ?

thank you very much :pray: :pray: :pray: :pray:

hi @limpeh78

first of all, you need to remove your old data using this.cart.splice(element) and then you need to push your updated data this.cart.push(element);

I think the element you are pushing to the array is the element you are iterating over in the ForEach method. So it is already there and then you are adding it again

noted, i give it a try using splice, thank you very much for your help

it works using your splice method first

let temp = element
temp[‘shippingcost’] = res[‘price’]
this.cart.push(element, 1);
this.cart.push(temp)

thank you once again :slightly_smiling_face: :pray:

ya, i try pushing outside the foreach, thank you for your help