0

The title pretty much sums it up. I am receiving an array from an API call, and storing it in a variable called this.ratesStore.aprRates. I then want to create a new array with the same data in it, called this.aprRates. However, no matter how I try, whenever I modify data in the "cloned" array, the other one is affected as well.

I've tried cloning the array via Object.assing, slice, Array.from, and spread operator, but I get the same issue every single time.

For instance, trying this:

console.log('apr rates - before:', this.aprRates);
console.log('rates store apr rates - before:', this.ratesStore.aprRates[0].terms[0].rate);

this.aprRates = this.ratesStore.aprRates.slice(0);

console.log('apr rates - after:', this.aprRates[0].terms[0].rate);
this.aprRates[0].terms[0].rate = 'chickens';
console.log('rates store apr rates - after:', this.ratesStore.aprRates[0].terms[0].rate);

Yields this output for me:

apr rates - before: undefined
rates store apr rates - before: 1.89
apr rates - after: 1.89
rates store apr rates - after: chickens

Any idea why this is happening?? I'm losing my mind over here. I'm writing this as part of an Angular component, if that makes any difference.

Thank you for your help!

Peter Rao
  • 21
  • 1
  • 3

0 Answers0