This code checks the background of a certain number of cells.
var array = sheet.getRange(1, 1, 20, 1).getBackgrounds();
If I log this array in the Google Apps Script console, it returns the following array:
[[#cccccc], [#ffffff], [#ffffff], [#ffffff], [#ffffff], [#cccccc], [#ffffff], [#ffffff], [#ffffff], [#ffffff], [#f3f3f3], [#ffffff], [#ffffff], [#ffffff], [#ffffff], [#ffffff], [#cccccc], [#cccccc], [#ffffff], [#ffffff]]
I want to check if this array does not contain a certain color code: #377a47
.
I tried to use includes
for this, however this does not work as the value is an array and not a string.
console.log(!array.includes("#377a47")); // returns true
This should return true, as the array does not contain this #377a47 color code. And it does return true, however it also returns true if I try the following color code that is present in the array:
console.log(!array.includes("#cccccc")); // returns true
So apparently this is not the right way to check if the array does not contain #377a47
. I suspected it had something to do with the fact it is a nested array, but am not sure how to correctly write the code to check this.
My Question: How to correctly check if the array does not contain the color code #377a47
?
CodePudding user response:
Arrays in JavaScript are compared by reference so even approach:
array.includes(["#377a47"]) //returns false
won't work
You can try flattening an array and compare primitive values like this:
console.log(![[1], [2], [3]].flat().includes(1)); //returns false