Remove empty elements from an array in Javascript

后端 未结 30 2600
无人共我
无人共我 2020-11-21 09:53

How do I remove empty elements from an array in JavaScript?

Is there a straightforward way, or do I need to loop through it and remove them manually?

30条回答
  •  爱一瞬间的悲伤
    2020-11-21 10:47

    What about this(ES6) : To remove Falsy value from an array.

    var arr = [0,1,2,"test","false",false,true,null,3,4,undefined,5,"end"];
    
    arr.filter((v) => (!!(v)==true));
    
    //output:
    
    //[1, 2, "test", "false", true, 3, 4, 5, "end"]
    

提交回复
热议问题