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.