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.
To remove the empty strings from an array, we can use the filter() method in JavaScript. In the above code, we have passed the callback function e => e to the filter method, so that it only keeps the elements which return true . empty "" string is falsy value, so it removes from the array.
Answer: Use the PHP array_filter() function You can simply use the PHP array_filter() function to remove or filter empty values from an array. This function typically filters the values of an array using a callback function.
1) Assigning it to a new empty array This is the fastest way to empty an array: a = []; This code assigned the array a to a new empty array. It works perfectly if you do not have any references to the original array.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With