13

Possible Duplicate:
Remove empty elements from an array in Javascript

I want to remove null or empty elements from an array using jquery

var clientName= new Array();
clientName[0] = "jack";
clientName[1] = "";
clientName[2] = "john";
clientName[2] = "peter";

Please give some suggestions.

Community
  • 1
  • 1
Kumaran
  • 1,156
  • 1
  • 9
  • 9
  • 4
    Have you tried anything? Are you asking for people to point you in the direction of loop statements and comparison operators? – lanzz Oct 05 '12 at 12:01
  • `{}` is an object definition list, not an array definition list. Which would be `[]`. – Mihai Stancu Oct 05 '12 at 12:03
  • @MihaiStancu Must be a typo, it would not be a valid object literal. – kapa Oct 05 '12 at 12:04
  • I tried to edit it to `[]`, but the OP insists it's `{}` :). The example code will throw a Syntax error. – kapa Oct 05 '12 at 12:17
  • The current edit looks OK. In the previous version: since JavaScript is a dynamic language it won't get upset if you create a new variable of type array, and then discard it by replacing it with a new variable type object. Besides that arrays are objects and object properties can be accessed using the array brackets. – Mihai Stancu Oct 05 '12 at 12:28
  • @MihaiStancu That's true, but `var x = { 1, 2, 3 };` is invalid code. You have to define the keys too (you are free to try it in your console, I did). The current edit is fine though, even if it reaches the same goal as my edit. – kapa Oct 05 '12 at 12:34

6 Answers6

22

Use the jquery grep function, it'll identify array elements that pass criteria you define

arr = jQuery.grep(arr, function(n, i){
  return (n !== "" && n != null);
});
Hans Hohenfeld
  • 1,729
  • 11
  • 14
5

There is no need in jQuery, use plain JavaScript (it is faster!):

var newArray = [];
for (var i = 0; i < clientname.length; i++) {
    if (clientname[i] !== "" && clientname[i] !== null) {
        newArray.push(clientname[i]);
    }
}
console.log(newArray);

Another simple solution for modern browsers (using Array filter() method):

clientname.filter(function(value) {
    return value !== "" && value !== null;
});
VisioN
  • 143,310
  • 32
  • 282
  • 281
3

Was thinking that since jQuery's .map() function relies on returning something not null / undefined, you can get away with just something like this:

var new_array = $.map(old_array, function (el) {
    return el !== '' ? el : null;
});

You still have to check for the empty string, but you really don't have to check for the null and undefined anymore, so that's one less complication in your logic.

Richard Neil Ilagan
  • 14,627
  • 5
  • 48
  • 66
  • From the manual: Within the function, `this` refers to the global (`window`) object. Also, you have to get rid of the `''`. The idea is good though. – kapa Oct 05 '12 at 12:38
  • 1
    @bažmegakapa ~ ah, good catch! Completely forgot about the empty string check, d'oh. If that's the case, this'll just reduce to any of the other answers here anyway. – Richard Neil Ilagan Oct 05 '12 at 12:42
  • Still +1, the idea is good. It's good that people don't have to check for `null` and `undefined`, because they seem to have enough problems with that, looking at the other answers :). – kapa Oct 05 '12 at 12:48
0
  1. Create a new empty array.
  2. Go into a foreach loop and add items to new array if value is not equal to ''.
-1

Try this :

$.each(clientname, function(key, value) { 
  if (value === 'undefined' || value === '')
     clientname.splice(key,1);
});
Laurent Brieu
  • 3,331
  • 1
  • 14
  • 15
-1

Use the following code:

var newarr=[];
for(var i=0; i<len(clientname);i++){

   if(clientname[i] !== "" && clientname[i] !== null){
    newarr.push(clientname[i]);
   }
}
XMen
  • 29,384
  • 41
  • 99
  • 151
  • This will remove `0` as well. – kapa Oct 05 '12 at 12:06
  • Have you tried? `0 == ''` in Javascript. – kapa Oct 05 '12 at 12:09
  • Sorry, but is still the same. No matter if you use this in your own code, because it's you who has to fix it, but please don't spread it on the Internet. [Hint](http://zero.milosz.ca/). – kapa Oct 05 '12 at 12:16
  • I am not your code interpreter. Please create a jsFiddle demo or use your console to test your own code. It won't run. – kapa Oct 05 '12 at 12:41