3

I have two javascript arrays. I need to copy the array values from one another. I have used code like below.

javascript

let firstArr = [{name: "one"},{name: "two"}];
let secondArr = [...firstArr];
secondArr[0].name = "three";
console.log(firstArr)
console.log(secondArr)

I have changed only "secondArr" values. But first array values are also changed. I have used the spread operator, so it will create a new memory then why the first array values are also changed?

Could you please anyone explain this. Thanks in advance.

iRohitBhatia
  • 3,039
  • 10
  • 48
  • 100
az rnd
  • 341
  • 5
  • 21

0 Answers0