views:

97

answers:

7

I need to check if array contains at least one empty elements. If any of the one element is empty then it will return false.

Example:

var my_arr = new Array(); 
my_arr[0] = ""; 
my_arr[1] = " hi ";
my_arr[2] = "";

The 0th and 2nd array elements are "empty".

+6  A: 

You can check by looping through the array with a simple for, like this:

function NoneEmpty(arr) {
  for(var i=0; i<arr.length; i++) {
    if(arr[i] === "") return false;
  }
  return true;
}

You can give it a try here, the reason we're not using .indexOf() here is lack of support in IE, otherwise it'd be even simpler like this:

function NoneEmpty(arr) {
  return arr.indexOf("") === -1;
}

But alas, IE doesn't support this function on arrays, at least not yet.

Nick Craver
Isn't this the reverse of what the OP asked for (returns true if the array has only empty elements, instead of returns false if any element is empty)?
Telemachus
Good call on the indexOf for array's. Now just let's hope someone from the IE dev team reads this and fix it !
fredrik
+1 for the jsfiddle link
Satoru.Logic
@Telemachus - Good catch, I missed his explicit "which result is false" requirement, updated :)
Nick Craver
Wouldn't it be better if we make `isEmpty` function the second parameter of `HasEmpty`, and the `arr[i] === ""` implementation as default.
Satoru.Logic
FWIW, which probably isn't much until we can drop support for <IE8, IE9 will support *indexOf()* and the other ECMAScript 5th array functions. http://blogs.msdn.com/b/ie/archive/2010/06/25/enhanced-scripting-in-ie9-ecmascript-5-support-and-more.aspx
Andy E
You could still use `indexOf` where it exists: `var arrayHasEmpty = Array.prototype.indexOf ? function(arr) { return arr.indexOf("") != -1; } : function(arr) { /* for loop stuff here */ };`
Tim Down
+1  A: 

You could do a simple help method for this:

function hasEmptyValues(ary) {
    var l = ary.length,
        i = 0;

    for (i = 0; i < l; i += 1) {
        if (!ary[i]) {
            return false;
        }
    }

    return true;
}

//check for empty
var isEmpty = hasEmptyValues(myArray);

EDIT: This checks for false, undefined, NaN, null, "" and 0.

EDIT2: Misread the true/false expectation.

..fredrik

fredrik
Also checks for `undefined`, right?
Telemachus
Yes indeed, miss on my part.
fredrik
+2  A: 

You have to check in through loop.

function checkArray(my_arr){
   for(var i=0;i<my_arr.length;i++){
       if(my_arr[i] === "")   
          return false;
   }
   return true;
}
Azhar
done the trick, thanks a lot
santanu
+3  A: 

You can try jQuery.inArray() function:

return jQuery.inArray("", my_arr)
Vitalii Fedorenko
Including jQuery for this seems *just* a bit overkill, don'tcha think?
Nick Craver
@Nick Fairly often it's already used in the project
Vitalii Fedorenko
@Nick Craver - yes, overkill if this is the only reason for using jQuery. On the other hand, when using jQuery this is a good solution :-)
gnud
yes this will also do, Thanks
santanu
@Vitalii - *Sometimes* it's used, it's still a minority, my point was you should qualify this type of answer on a question not tagged jQuery with something like "If you're already using jQuery..." A novice finding this later and including jQuery for *just* this would be a bad thing.
Nick Craver
@Nick Usually, on SO for each question we have several available answers with their own pros and cons, and even a novice will be able to think through all available solutions and choose an answer that is appropriate for him.
Vitalii Fedorenko
@Vitalii - I think you misunderstood, I meant a JS novice, not an SO one. By definition, they don't know what's best :)
Nick Craver
@Nick Problem of adding extra dependency to your project is not related to knowledge of Javascript, I think it's obvious that before using a new framework you should evaluate pay and benefits.
Vitalii Fedorenko
A: 

I see in your comments beneath the question that the code example you give is PHP, so I was wondering if you were actually going for the PHP one? In PHP it would be:

function hasEmpty($array)
{
  foreach($array as $bit)
  {
    if(empty($bit)) return true;
  }

  return false;
}

Otherwise if you actually did need JavaScript, I refer to Nick Craver's answer

Johannes Jensen
A: 
function containsEmpty(a) {
    return [].concat(a).sort().reverse().pop() === "";
}
alert(containsEmpty(['1','','qwerty','100'])); // true
alert(containsEmpty(['1','2','qwerty','100'])); // false
serious
A: 

Just do a len(my_arr[i]) == 0; inside a loop to check if string is empty or not.

pymendoza