'Change variable in array or list
I'm trying to change second element in my array using loop in this way:
myTab = [false,true,true];
if (trueOfFalse){
for (let i = 0; i < myTab.length; i++) {
myTab[i].set(true)
}
myTab[3] = false;
How to fix it for running ? In Java this works.
Solution 1:[1]
Your question doesn't really make sense but here is some JavaScript that will change the second item in the myTab
array to be false
. I'm sure you can adapt it how you see fit.
const myTab = [false, true, true];
for (let i = 0, len = myTab.length; i < len; i++) {
if (i === 1) {
myTab[i] = false;
}
}
// Log the modified array
console.log(myTab);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |