Remove empty elements from an array in Javascript

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

    var data= { 
        myAction: function(array){
            return array.filter(function(el){
               return (el !== (undefined || null || ''));
            }).join(" ");
        }
    }; 
    var string = data.myAction(["I", "am","", "working", "", "on","", "nodejs", "" ]);
    console.log(string);
    

    Output:

    I am working on nodejs

    It will remove empty element from array and display other element.

提交回复
热议问题