How do I remove null values from an array within an array?

梦想的初衷 提交于 2020-07-10 07:34:09

问题


I am working with Google Sheets and Google Apps Scripting. I have used .getRange().getValues to grab user emails from a Sheet.

Example Array Received: [[user1, , ], [user2, user3, ], [user4, user5, user6]]

As you can see, I have a potential of up to three users per row (inner array). However, some of those cells are empty resulting in empty values in the array. I am then feeding each inner array into .addEditors() which throws an error due to the empty user.

I know I can run through each array and delete the empties and then push that new array into .addEditors() but it would be ugly, and very inefficient. I don't have enough experience to know how to develop a more elegant way.

Could someone help me understand the how and the why to solve this issue in as efficient a manner as possible? Thanks.

Note: Though it seems like the .filter(Boolean) solution might work, I can only get it to work for a single array, not for an array within an array.

var myFilterArray = myArray.filter(Boolean);

    arr.forEach(function(x){
      return x.filter(Boolean);
    });

I cannot get it to return the modified array if greater than one deep.


回答1:


I know I can run through each array and delete the empties... thats pretty much your only options. There are methods that make your code look cleaner but behind the scenes something is going to have to loop through the array.

Here is an example using Array.map() and Array.filter()

let data = [["bob", , ], ["jake", "john", ""], ["joe", "henry", "morgan"]];
let newData = data.map(x => {
  return x.filter(j => j)
})
console.log(newData);

Using ES5 syntax

var data = [["bob", , ], ["jake", "john", ""], ["joe", "henry", "morgan"]];
var newData = data.map(function(x) {
  return x.filter(function(j) {if (j) return j})
})
console.log(newData);



回答2:


If You have following type of array then we can rid of empty data using filter method.

var data=[["user1", , ], ["user2", "user3", ], ["user4", "user5", "user6"]];

data.filter(a => a.filter( b => b.length>0));



回答3:


Just out of curiosity I ran these four functions 10 times each on 3 columns and 1000 rows of data. Partially, I was interested in learning more about these array methods because I've just began to use them. So I'm not expert at them for sure.

The code:

function removeFalsies1() {
  var start=new Date();
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  var vU=[];
  var cnt=0;
  for(var i=1;i<vA.length;i++){
    for(var j=0;j<vA[i].length;j++) {
      cnt++;
      if(vA[i][j]) {
        vU.push(vA[i][j]);
      }
    }
  }
  var end=new Date();
  return Utilities.formatString('1,%s',seconds_(start,end));
}

function removeFalsies2() {
  var start=new Date();
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  var cnt=0;
  var vU=[];
  vA.forEach(function(r,i,A){ 
    if(i>0) {
      for(var n=0;n<A[i].length;n++){
        var rA=[];
        cnt++;
        if(A[i][n] && i>0){
          vU.push(A[i][n]);
        }
      }
    }
    return rA;
  });
  var end=new Date();
  return Utilities.formatString('2,%s',seconds_(start,end));
}

function removeFalsies3() {
  var start=new Date();
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  var cnt=0;
  var vU=[];
  vA.forEach(function(r,i,A){if(i>0){r.forEach(function(rix,ix,r){if(rix){vU.push(rix);}cnt++;})}});
  var end=new Date();
  return Utilities.formatString('3,%s',seconds_(start,end));
}

function removeFalsies4() {
  var start=new Date();
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  var cnt=0;
  var vU=[];
  vA.map(function(r,i) {if(i>0){return r.filter(function(rix){if(rix){vU.push(rix);}cnt++;})}});
  var end=new Date();
  return Utilities.formatString('4,%s',seconds_(start,end));
}

The Data:

The single digit numbers refer the last digit in the function names.



来源:https://stackoverflow.com/questions/55796457/how-do-i-remove-null-values-from-an-array-within-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!