filtering 2d arrays using javascript

前端 未结 5 2043
眼角桃花
眼角桃花 2021-01-28 06:23

I have a 2D array where I need to filter the rows having date field (3d column)

var data = [
[\'1\',\'a\',\'12-12-2019\',\'A\'],
[\'2\',\'b\',\'\',\'A\'],
[\'3\'         


        
5条回答
  •  旧巷少年郎
    2021-01-28 07:20

    I wouldn't worry about using a loop to do that - this is what loops are for.

    You could just use Array.prototype.filter() to make sure that there's a value in the 2nd position of each array, returning whether it's truthy or not.

    var data = [
      ['1','a','12-12-2019','A'],
      ['2','b','','A'],
      ['3','c','12-1-2019','A'],
      ['4','d','','A'],
    ];
    
    // Used a classic `function` keyword because you're using this in a Google apps script
    const result = data.filter(function(item) {
      return item[2];
    });
    
    console.log(result);
    

提交回复
热议问题