I'm trying a truly easy thing which is looping inside a 2D array and creating another array by checking row per row.
For example, my 2D array contains the hex colors of a range of cells from a Google Sheet. I want to check every line and verify if on of the element id either red or orange.
Here is my sample code :
var 2Darray = [ [ '#ff0000', '#000000', '#000000' ],
[ '#000000', '#ff6d01', '#000000' ],
[ '#000000', '#000000', '#000000' ] ]
var redHex = "#ff0000";
var orangeHex = "#ff6d01";
var newColors = [];
for (var i = 0; i < rangeColor.length; i++) {
var keepGoing = true;
for (var j = 0; j < rangeColor[i].length; j++) {
switch (rangeColor[i][j]) {
case redHex:
newColors[i] = redHex;
console.log("1")
keepGoing = false;
break;
case orangeHex:
newColors[i] = orangeHex;
console.log("2");
keepGoing = false;
break;
case "#000000":
newColors[i] = "#000000";
console.log("3")
keepGoing = false;
break;
}
if (!keepGoing) {
break;
}
}
}
console.log(newColors);
My new array should be : [ '#ff0000', '#ff6d01', '#000000' ] but is [ '#ff0000', '#000000', '#000000' ] which indicates that there is an issue with my loop but I can't figure out what. Maybe the break behavior with double for loop ?
rangeColor.length?2DarrayisrangeColor, in your script, how about removingkeepGoing = false;incase "#000000":? Because when the 2nd element of the array is checked, only the 1st element is checked becausekeepGoing = false;atcase "#000000":. If2DarrayisrangeColor, I think that by this modification,[ '#ff0000', '#ff6d01', '#000000' ]is obtained. If I misunderstood your script, I apologize.