简体   繁体   中英

JS Remove Entries from 2D array

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

In your current if condition, undefined || '' || null undefined || '' || null 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){

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:

  1. sumRng[i][2] is evaluated; let's call the resulting value left
  2. undefined || '' undefined || '' is evaluated, the result is '' ; let's call it temp ¹
  3. temp || null temp || null ( '' || null ) is evaluated, the result is null ; let's call that right
  4. The result of left !== right is evaluated

So 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 || '' undefined || '' result in '' ? Because an expression a || b a || b is evaluated like this:

  1. Evaluate a
  2. If the value from Step 1 is truthy , make that the result of the || operation
  3. Otherwise, evaluate b and make that the result of the || operation

undefined 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 .)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM