Array of splice and console.log

When I perform operations on an array in javascript, console.log shows that the array has already been modified BEFORE I perform operations on the array. I can reproduce this in chrome and firefox. Does anyone know why this is?

var myTabs = [[0,0,0],
              [0,0,0],
              [0,0,0],
              [0,0,0]];
console.log(myTabs);
myTabs[0].splice(1, 1);
console.log(myTabs);

See this for code:

https://jsfiddle.net/mxrh33t0/1/

+4
source share
1 answer

When you expand the registered object in Chrome, you expand the last link to it, and not a copy of this object at the time of registration.

In your case, the last reference was to the array after the method call splice(). If you want to check the difference, you need to be more specific when registering:

var myTabs = [[0,0,0],
              [0,0,0],
              [0,0,0],
              [0,0,0]];
console.log(myTabs[0].join(","));
myTabs[0].splice(1, 1);
console.log(myTabs[0].join(","));

You can expand it if you really want to see more.

+5

Source: https://habr.com/ru/post/1692272/


All Articles