Reputation: 601
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
Upvotes: 0
Views: 750
Reputation: 601
I still struggle with the why the original OR method I had doesn't work. In fact I went over to reddit to try and see if I could get anymore clarification but still it won't go in. But I appreciate all your help trying.
I did get an alternative solution to my conundrum though which seems a more condensed version of what I was using. That was simply this (courtesy of user insertAlias)
if(sumRng[i][2]){}
Apparently that will only pass for anything truthy so seems to fit the bill. But please point out any shortcomings
Upvotes: 1
Reputation: 1074168
Assuming you've defined micSumm
somewhere and started out with a blank array in it (var micSumm = [];
), then you're on the right track, the issue is here:
if(sumRng[i][2] !== (undefined || '' || null)){
That's not how you do a check against multiple values in JavaScript. The way that's evaluated is:
sumRng[i][2]
is evaluated; let's call the resulting value left
undefined || ''
is evaluated, the result is ''
; let's call it temp
¹temp || null
('' || null
) is evaluated, the result is null
; let's call that right
left !== right
is evaluatedSo you end up only checking for null
, not undefined
or ''
.
Instead:
var value = sumRng[i][2];
if (value !== undefined && value !== '' && value !== null) {
Or you can take advantage of the fact != undefined
also checks for null
:
var value = sumRng[i][2];
if (value != undefined && value !== '') {
...but that can be a bit less clear.
¹ Why does undefined || ''
result in ''
? Because an expression a || b
is evaluated like this:
a
||
operationb
and make that the result of the ||
operationundefined
is falsy, not truthy. (A falsy value is any value that coerces to false
when used as a boolean. The falsy values are undefined
, null
, ""
, 0
, NaN
, and of course, false
. All other values are truthy.)
Upvotes: 3
Reputation: 50445
You could also use filter(which is supported by apps-script):
var filteredArr = sumRng.filter(function(e) {return e[2];})
Note that you're getting a 2D array with a specific rectangular dimension. So, there's no possibility of a value being undefined
. Unless you specifically made a value null
,null
isn't returned. As noted in the comments below, the above also filters out 0 and boolean false. So, You can use
sumRng.filter(function(e) {return e[2].toString();})
or
sumRng.filter(function(e) {return e[2] || e[2] === false || e[2] === 0;})
References:
Array#filter
Primitives
null
Comparison Operators
Truthy
Upvotes: 2
Reputation: 28455
In your current if
condition, undefined || '' || null
evaluates to null
. Hence, the condition eventually becomes sumRng[i][2] !== null
.
However, as you need to check for undefined
and ''
too, you will need to update your condition
From
if(sumRng[i][2] !== (undefined || '' || null)){
to
if(sumRng[i][2] !== undefined && sumRng[i][2] !== '' && sumRng[i][2] !== null){
Upvotes: 3