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.

6
  • 4
    Have you tried anything? Are you asking for people to point you in the direction of loop statements and comparison operators? Commented Oct 5, 2012 at 12:01
  • {} is an object definition list, not an array definition list. Which would be []. Commented Oct 5, 2012 at 12:03
  • @MihaiStancu Must be a typo, it would not be a valid object literal. Commented Oct 5, 2012 at 12:04
  • I tried to edit it to [], but the OP insists it's {} :). The example code will throw a Syntax error. Commented Oct 5, 2012 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. Commented Oct 5, 2012 at 12:28

6 Answers 6

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);
});
Sign up to request clarification or add additional context in comments.

4 Comments

You cann add that to ie. ... return(n !== "" && n != null && n !== 0)...
+1 Much better with !==. grep is a good option by the way if one uses jQuery anyways.
@bažmegakapa thank you for reminding me implicitly about the difference between '!==' and '!=' :)
may be return (n ? true: false) is more efficient?
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;
});

Comments

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.

3 Comments

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.
@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.
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 :).
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 ''.

Comments

-1

Try this :

$.each(clientname, function(key, value) { 
  if (value === 'undefined' || value === '')
     clientname.splice(key,1);
});

3 Comments

This will remove 0 as well.
Yes @bažmegakapa, but the array doesn't seems to contain numeric values. I've made a fix.
Even worse :). 0 == ''. And what about that 'undefined'?
-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]);
   }
}

4 Comments

This will remove 0 as well.
Have you tried? 0 == '' in Javascript.
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.
I am not your code interpreter. Please create a jsFiddle demo or use your console to test your own code. It won't run.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.