【问题标题】:How to remove empty arrays from multidimensional array in JavaScript?如何从 JavaScript 中的多维数组中删除空数组?
【发布时间】:2021-01-19 02:25:20
【问题描述】:

我在 google 表格中为我的搜索应用程序从多维数组中删除空数组/对象时遇到问题。我正在使用getLastRow,所以我想我会避免这个问题,但不幸的是它不是。

我的数组如下所示:

[[1.39080000912E11, RSSMA004025, ボタンキャップ M4X25, 4.0, 4.2, , , ], [1.39080000912E11, RWJAA058068, FTB-268, 1.0, 486.0, 486.0, , ], [, , , , , , , ], [, , , , , , , ]]

我正在寻找可以从数组末尾删除空数组的东西,所以它应该是这样的:

[[1.39080000912E11, RSSMA004025, ボタンキャップ M4X25, 4.0, 4.2, , , ], [1.39080000912E11, RWJAA058068, FTB-268, 1.0, 486.0, 486.0, , ]]

它只是整个数组的一部分,但我希望你能理解我在寻找什么。

到目前为止我的代码:

function getDataForSearch(){
    
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getSheetByName("WP");
  
  var test1 = ws.getRange(4, 6, ws.getLastRow(),8).getValues();

const arrFiltered = test1.filter(function(x){
  return (x !== (undefined || null || ''));
});

 Logger.log(arrFiltered);

}

【问题讨论】:

  • 在你的函数const arrFiltered = test1.filter(function(x){ return (x !== (undefined || null || '')); });中,x 是一个数组。不是一个对象。

标签: javascript google-apps-script google-sheets


【解决方案1】:

你的函数应该是这样的

function getDataForSearch() {

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getSheetByName("WP");

  var test1 = ws.getRange(4, 6, ws.getLastRow(), 8).getValues();

  const arrFiltered = test1.filter(function (x) { /* here, x is an array, not an object */
    return !(x.every(element => element === (undefined || null || '')))
  });

  Logger.log(arrFiltered);

}

【讨论】:

    【解决方案2】:

    我正在使用 getLastRow,所以我想我会避免这个问题,但不幸的是它不是。

    getRange 语法是

    getRange(row, column, numRows, numColumns)
    

    脚本正在使用

    ws.getRange(4, 6, ws.getLastRow(), 8).getValues();
    

    第三个参数是numRows- 行数。如果最后一行是 10,则您将获得从第 4 行到第 14 行(4+10)的范围。要在第 10 行结束,行数应该是 7。因此正确的范围应该是

    ws.getRange(1 + 3, 6, ws.getLastRow() - 3, 8).getValues();
    

    这种空行的另一种可能性是传统使用数组公式。见ArrayFormula is breaking the getLastRow() funtion. Possible workarounds?

    【讨论】:

    • 是的,我正在考虑,但整个数组有 10 个非空元素和 100 个空元素,所以我想这不是问题
    • @Timonek 编辑了我的答案以提供另一种可能性。
    • 所以单元格中的公式弄乱了getLastRow()....下次我会记住的
    【解决方案3】:

    你想使用 filter() 和 some()

    var arr = [[1.39080000912E11, 'RSSMA004025', 'ボタンキャップ M4X25', 4.0, 4.2, , , ], [1.39080000912E11, 'RWJAA058068', 'FTB-268', 1.0, 486.0, 486.0, , ], [, , , , , , , ], [, , , , , , , ]];
    
    // Will fail for falsly values, but works in this case
    var updated1 = arr.filter(function (a) {
      return a.some(Boolean);
    });
    
    // var updated1 = arr.filter(a => a.some(Boolean));
    console.log(updated1);
    
    // check for undefined
    var updated2 = arr.filter(function (a) {
      return a.some(function (val) {
        return val !== undefined;
      });;
    });
    
    // var updated2 = arr.filter(a => a.some(val => val !== undefined));
    console.log(updated2);
    
    //If it can be any of the values
    var empty = [undefined, null, ''];
    var updated3 = arr.filter(function (a) {
      return a.some(function (val) {
        return !empty.includes(val);
      });
    });
    
    // var updated3 = arr.filter(a => a.some(val => !empty.includes(val)));
    console.log(updated3);

    【讨论】:

    • 感谢您的回答!对我来说,使用Boolean 的方法非常完美。我不知道为什么undefined 根本不起作用......你认为这是因为在一些空元素中有没有任何输出的公式吗?
    【解决方案4】:

    您可以加入数组中的元素,然后检查它们的长度以进行过滤

    let test1 = [[1.39080000912E11, 'RSSMA004025', 'uiuiu M4X25', 4.0, 4.2, , , ],[1.39080000912E11, 'RWJAA058068;', 'FTB - 268', 1.0, 486.0, 486.0, , ],[, , , , , , , ],[, , , , , , , ]]
    
    
    const arrFiltered = test1.filter(function(x) {
      return (x.join('').length !== 0);
    });
    
    console.log('new array',arrFiltered);

    【讨论】:

      【解决方案5】:

      也许可以试试这样的东西。您只想删除所有数组元素都未定义的数组。

      var funnyArrays = [['a', 'b', 'c'],  [,,,], ['d','e','f'], [,,,]];
      
      var fullArrays = funnyArrays.filter(a => !a.every(ax=> a == undefined));
      

      【讨论】:

        【解决方案6】:

        你可以试试。

        const notEmptyArrayOrObject = (arr)=> {
        
          if(typeof arr !== 'object'){ return true }
        
          if(!Array.isArray(arr){
             arr = Object.values(arr)
          }
        
          return arr.filter(item=>item === undefined).length !== arr.length
        }
        
        const filterEmptyFromArray = (array)=>array.filter(notEmptyArrayOrObject)
        

        【讨论】:

          猜你喜欢
          • 2016-04-25
          • 1970-01-01
          • 2014-02-13
          • 1970-01-01
          • 1970-01-01
          • 2022-12-10
          • 2017-09-27
          • 2021-01-23
          • 1970-01-01
          相关资源
          最近更新 更多