remove an empty string from array of strings - JQuery

If you use Javascript 1.6 (probably wont work on IE8 or less) you can use

arr.filter(Boolean) //filters all non-true values

eg.

console.log([0, 1, false, "", undefined, null, "Lorem"].filter(Boolean));
// [1, "Lorem"]

You may use filter :

var newArray = oldArray.filter(function(v){return v!==''});

The MDN has a workaround for IE8 compatibility. You might also use a good old loop if you're not going to use filter anywhere else, there's no problem with looping...


Another alternative is to use the jquery's .map() function:

var newArray = $.map( oldArray, function(v){
  return v === "" ? null : v;
});

Tags:

Arrays

Jquery