0

the objects still changing their value,if something that i losing can you give me an article or the documentation about that, for example:

const array = [
  [
    { local: '5', visit: '6' },
  ],
  [
    { local: '6', visit: '1' },
  ],
  [
    { local: '8', visit: '6' },
  ],
  [
    { local: '6', visit: '2' },
  ],
  [
    { local: '3', visit: '6' },
  ],
  [
    { local: '6', visit: '7' },
  ],
  [
    { local: '4', visit: '6' },
  ]
];

i tried with:

array.map(element => element.slice());

3 Answers3

1
let copied_array = [...array];
Vivek Bani
  • 3,703
  • 1
  • 9
  • 18
1

To clone an array with objects inside property, you need to perform a deep copy. Probably simplest with:

const newArray = JSON.parse(JSON.stringify(array));
Samuel Olekšák
  • 389
  • 4
  • 12
0

try with this:

const copy = array.map((row) => row.slice().map((object2Copy) => Object.assign({}, object2Copy)));