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.

Alwaysblue
  • 9,948
  • 38
  • 121
  • 210
az rnd
  • 643
  • 3
  • 14
  • 28

0 Answers0