Remove all falsy values from an array

后端 未结 22 3296
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 07:21

I would like to remove all falsy values from an array. Falsy values in JavaScript are false, null, 0, \"\", undefined, and NaN.



        
22条回答
  •  旧时难觅i
    2020-11-28 07:53

    I see you never accepted an answer. Is the problem that you are relying on Logger.log or console.log to see if the null removal worked? I think the filter suggested by @LoremIpsum is the cleanest solution.

      const src2DArr = [[34], [75], [30], [48], [976], [], [178], [473], [51], [75], [29], [47], [40]];
      Logger.log("src2DArr: " +JSON.stringify(src2DArr)); 
      // [[34],[75],[30],[48],[976],[],[178],[473],[51],[75],[29],[47],[40]]
      
      var src2DArr1 = src2DArr.filter(Boolean);
      Logger.log("src2DArr1: " + JSON.stringify(src2DArr1));
      // [[34],[75],[30],[48],[976],[],[178],[473],[51],[75],[29],[47],[40]] 
    

提交回复
热议问题