Remove empty elements from an array in Javascript

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

    You should use filter to get array without empty elements. Example on ES6

    const array = [1, 32, 2, undefined, 3];
    const newArray = array.filter(arr => arr);
    

提交回复
热议问题