Remove empty elements from an array in Javascript

后端 未结 30 2494
无人共我
无人共我 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:36

    Since nobody else mentioned it and most people have underscore included in their project you can also use _.without(array, *values);.

    _.without(["text", "string", null, null, null, "text"], null)
    // => ["text", "string", "text"]
    

提交回复
热议问题