Remove empty elements from an array in Javascript

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

    You can use filter with index and in operator

    let a = [1,,2,,,3];
    
    console.log(a);
    
    let b = a.filter((x,i)=> i in a);
    
    console.log(b);

提交回复
热议问题