Remove empty elements from an array in Javascript

后端 未结 30 2593
无人共我
无人共我 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条回答
  •  -上瘾入骨i
    2020-11-21 10:37

    The clean way to do it.

    var arr = [0,1,2,"Thomas","false",false,true,null,3,4,undefined,5,"end"];
    arr = arr.filter(Boolean);
    // [1, 2, "Thomas", "false", true, 3, 4, 5, "end"]
    

提交回复
热议问题