js array removes invalid values (null, undefined)

  1. Do not filter Boolean values ​​and empty strings
var arr=[1,2,null,undefined,9,0,NaN,,'',true];

arr=arr.filter(x=>!!x==true||x==0);   //[1, 2, 9, 0,'',true]

2. Boolean can be filtered, custom filter rules

let arr = ["",false,NaN,null,undefined,"123", ,"test",123,true];
let newarr = arr.filter(item=>!["",null,undefined,NaN,false,true].includes(item));
console.log(arr,newarr)
//  ['', false, NaN, null, undefined, '123', empty, 'test', 123, true] 
//  ['123', 'test', 123]

Welcome everyone to pay attention to my official account "Treading the Waves Life Circle". There are not only more interesting content here, but also the  latest information on Treading the Waves will be updated synchronously, so that you can always keep abreast of the cutting-edge technology. Come and join us!

Guess you like

Origin blog.csdn.net/weixin_45849072/article/details/120449439