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?
    – lanzz
    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.
    – kapa
    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.
    – kapa
    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);
});
4
  • You cann add that to ie. ... return(n !== "" && n != null && n !== 0)... Commented Oct 5, 2012 at 12:08
  • +1 Much better with !==. grep is a good option by the way if one uses jQuery anyways.
    – kapa
    Commented Oct 5, 2012 at 12:12
  • 1
    @bažmegakapa thank you for reminding me implicitly about the difference between '!==' and '!=' :) Commented Oct 5, 2012 at 12:15
  • 1
    may be return (n ? true: false) is more efficient?
    – chromigo
    Commented Apr 7, 2016 at 10:50
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;
});
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
  • 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
    Commented Oct 5, 2012 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. Commented Oct 5, 2012 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
    Commented Oct 5, 2012 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);
});
3
  • This will remove 0 as well.
    – kapa
    Commented Oct 5, 2012 at 12:05
  • Yes @bažmegakapa, but the array doesn't seems to contain numeric values. I've made a fix. Commented Oct 5, 2012 at 12:08
  • Even worse :). 0 == ''. And what about that 'undefined'?
    – kapa
    Commented Oct 5, 2012 at 12:10
-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
  • This will remove 0 as well.
    – kapa
    Commented Oct 5, 2012 at 12:06
  • Have you tried? 0 == '' in Javascript.
    – kapa
    Commented Oct 5, 2012 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.
    – kapa
    Commented Oct 5, 2012 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
    Commented Oct 5, 2012 at 12:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.