Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

javascript - 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 indexes 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...