Delete empty lines from array while keeping record without loop?

This question was asked here: Remove empty rows from an array while maintaining a record of indices with non-empty rows

If you noticed that this @Baz posted it;

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

"and from this I want to create the following two arrays:"

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

All answers to this question relate to the cycle form.

My question is: is it possible to delete all index es using empty string without any loops? ... is there any other alternative than repeating the array?

Maybe there are some regex or some jQuery that we don’t know about?

All answers or suggestions are highly appreciated.

+80
javascript string arrays indexing removeall
Nov 10 '13 at
source share
6 answers
 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 Function Documentation

+275
Nov 10 '13 at
source share
 var newArray = oldArray.filter(function(v){return v!==''}); 
+18
Mar 11 '14 at 21:34
source share

PLEASE NOTE: The documentation states:

filter - extension of JavaScript standard ECMA-262; as such, it may not be present in other implementations of the standard . You can get around this by inserting the following code at the beginning of your scripts, allowing you to use the filter in ECMA-262 implementations that do not support it. This algorithm is exactly that specified in ECMA-262, 5th edition, assuming that fn.call evaluates the original value of Function.prototype.call, and that Array.prototype.push has its original value.

So, to avoid some heartache, you might have to add this code to your script at the beginning .

 if (!Array.prototype.filter) { Array.prototype.filter = function (fn, context) { var i, value, result = [], length; if (!this || typeof fn !== 'function' || (fn instanceof RegExp)) { throw new TypeError(); } length = this.length; for (i = 0; i < length; i++) { if (this.hasOwnProperty(i)) { value = this[i]; if (fn.call(context, value, i, this)) { result.push(value); } } } return result; }; } 
+8
Nov 10 '13 at 10:52
source share

If you are using jQuery, grep may be useful:




 var arr = [ a, b, c, , e, f, , g, h ]; arr = jQuery.grep(arr, function(n){ return (n); }); 

arr now [ a, b, c, d, e, f, g];

+2
Oct 28 '14 at 14:18
source share
 arr = arr.filter(v => v); 

as returned v is implicitly converted to truthy

0
Jan 11 '19 at 3:27
source share

ie we need to take several email addresses, separated by comma, spaces or a new line, as shown below.

  var emails = EmailText.replace(","," ").replace("\n"," ").replace(" ","").split(" "); for(var i in emails) emails[i] = emails[i].replace(/(\r\n|\n|\r)/gm,""); emails.filter(Boolean); console.log(emails); 
-one
Sep 15 '16 at 12:38
source share



All Articles