Why does the foreach loop affect the entire large array?

clipboard.png
totaltab targetNametotaltab[targetName].contactdatatotaltabstate
total
clipboard.png

:

clipboard.png
for example, if you take totaltab [0] .contactdata = = 1, then the two-dimensional array of the first large object in totaltab will become 1, which will not affect the whole totaltab, but foreach will

Mar.06,2021

means that every element in your totaltab is actually the same element, such as

let obj = { a: 1 };
let arr = [obj, obj, obj];
arr.forEach(v => console.log(v));
arr[0].a = 2;
arr.forEach(v => console.log(v));
< hr >

if you say that executing totaltab [0] .contactdata = 1 will only make totaltab [0] .contactdata become 1 but not totaltab [1] .contactdata also becomes 1 , that can only mean totaltab [0]! = totaltab [1] , but not totaltab [0] .contactdata [I]

console.log(totaltab[0].contactdata[0][0] == totaltab[0].contactdata[0][1]);
console.log(totaltab[0].contactdata[0][0] == totaltab[1].contactdata[0][0]);
< hr >

post the text of your code later, not just screenshots, so that others can copy your code to test

I wrote a test code myself:

let totaltab = [
  {
    Extra: null,
    contactdata: [
      [
        {
          state: 2
        },
        {
          state: -1
        },
        {
          state: -2
        },
        {
          state: 1
        }
      ],
      [
        {
          state: -5
        },
        {
          state: 2
        },
        {
          state: -4
        },
        {
          state: 0
        }
      ]
    ]
  },
  {
    Extra: null,
    contactdata: [
      [
        {
          state: -2
        },
        {
          state: -1
        },
        {
          state: -2
        },
        {
          state: 1
        }
      ],
      [
        {
          state: 3
        },
        {
          state: -2
        },
        {
          state: 1
        },
        {
          state: 0
        }
      ]
    ]
  }
];

let tabtarget = totaltab[0].contactdata;
tabtarget.forEach(item => {
  item.forEach(items => {
    items.state = items.state >= 1 ? 0 : items.state;
  });
});

console.log(JSON.stringify(totaltab, ' ', 2));

see for yourself, there is no problem.

but if each of your items is actually equal, what you say happens:

let item = { state: 2 };
let totaltab = [
  {
    Extra: null,
    contactdata: [
      [ item, item, item, item ],
      [ item, item, item, item ],
    ]
  },
  {
    Extra: null,
    contactdata: [
      [ item, item, item, item ],
      [ item, item, item, item ],
    ]
  }
];

let tabtarget = totaltab[0].contactdata;
tabtarget.forEach(item => {
  item.forEach(items => {
    items.state = items.state >= 1 ? 0 : items.state;
  });
});

console.log(JSON.stringify(totaltab, ' ', 2));
< hr >

in addition, you may have noticed that the traversal code I wrote later is not quite the same as yours, but my code is equivalent to yours, and yours will go around a little more. And the variables in js are generally named with a small hump. It is recommended to use totalTab and tabTarget instead of totaltab and tabtarget . It doesn't matter if the short identifier is in full lowercase, it's ugly when it's long. And the variable name you traverse in two layers doesn't look very logical. item is an array, and its element is called items , which looks uncomfortable.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb44c7-24cb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb44c7-24cb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?