Totally newbie to JS so apologise for the probably obvious solution to this issue!
I'm trying to write a bit of code for use in google sheets that basically removes elements of a 2D array if the 3rd value in each nested array is empty eg.
[[1,2,3],[4,5,,],[7,8,9],[,,,]
would become
[[1,2,3],[7,8,9]]
I've currently got:
if (bkLoc=='HALL'){
var sumRng = sh.getRange('MIC_SUMM').getValues();
for (var i =0; i<sumRng.length ; i++){
if(sumRng[i][2] !== (undefined || '' || null)){
micSumm.push(sumRng[i]);
}
}
}
But the output seems to contains loads of empty arrays almost like its pushing every loop and I'm not sure why.
Any help would be gratefully received!
EDIT1: So with the help of you guys I got it to work. Using Nikhils answer I am now using this for the IF
if (bkLoc=='HALL'){
var sumRng = sh.getRange('MIC_SUMM').getValues();
for (j =0; j<sumRng.length ; j++){
var x = sumRng[j][2];
if(x != 0 && x != '??' && x != undefined){
micSumm.push(sumRng[j]);
}
}
}
But to be honest I don't really understand it. My understanding was || is OR so in my original code
if(sumRng[i][2] !== (undefined || '' || null))
If the tested content DOESN'T CONTAIN undefined OR "" OR null , the if statement should be true. I thought && meant AND so I'm unclear as to why that ever passes
Apologies for being so dumb!
Cheers
Array.filters()function.sumRng[i][2] !== (undefined || '' || null))is really wrong.Array.filtersfunction. If you meantArray#filter(singular), not in GAS, see my first comment above.if (sumRng[i][2] !== (undefined || '' || null), the(undefined || '' || null)is evaluated first to('' || null)and then to(null)and thennull, and then it is compared tosumRng[i][2]. This is because the logical OR returns the RHS if the LHS is not evaluable totrueI.e. it becomesif (sumRng[i][2] !== null). The innermost element result ofRange#getValues()will have either a value, or"", nevernull, so that statement will always be true.