Universal Grasp Universal Grasp - 1 year ago 59
Javascript Question

Remove empty strings from array while keeping record Without Loop?

This question was asked here:
Remove empty strings from array while keeping record of indexes with non empty strings

If you'd notice the given as @Baz layed it out;

"I", "am", "", "still", "here", "", "man"


"and from this I wish to produce the following two arrays:"

"I", "am", "still", "here", "man"


All the Answers to this question referred to a form of looping.

My question: Is there a possibility to remove all
index
es with
empty
string
without any looping? ... is there any other alternative apart from iterating the array?

May be some
regex
or some
jQuery
that we are not aware of?

All answers or suggestions are highly appreciated.

Answer Source
var arr = ["I", "am", "", "still", "here", "", "man"]
// arr = ["I", "am", "", "still", "here", "", "man"]
arr = arr.filter(Boolean)
// arr = ["I", "am", "still", "here", "man"]

filter documentation


// arr = ["I", "am", "", "still", "here", "", "man"]
arr = arr.filter(v=>v!='');
// arr = ["I", "am", "still", "here", "man"]

Arrow functions documentation