views:

56

answers:

1

I'm working with JQuery to determine if an array, built earlier, with a determined number of indexes, is full or not. When created, array looks like this :

,,,,,,

All I want to do is find if every position is filled or not.

So bascially, i'm trying to test if and only if

[x,x,x,x,x,x]

For example, if

[x,x,x,,x,x]  or if [x,,,,,] //return false, wrong...

Thanks a lot.

+5  A: 

You don't need any specific jQuery stuff to read the size of an array. Just vanilla javascript has what you need.

var arrayMaxValues = 4;
var testArray = [1,2,3,4];

if ( testArray.length == arrayMaxValues )
{
  alert( 'Array is full!' );
}
else if ( testArray.length > arrayMaxValues )
{
  alert( 'Array is overstuffed!' );
} else {
  alert( 'There's plenty of room!' );
}

EDIT

Ah, you re-typed your question. So, you want to see if they array has zero null or undefined values?

function isArrayFull( arr )
{
  for ( var i = 0, l = arr.length; i < l; i++ )
  {
    if ( 'undefined' == typeof arr[i] || null === arr[i] )
    {
      return false
    }
  }
  return true;
}
Peter Bailey
I think what he's after is more like trying to figure out if any of the values in the array are null or the empty string.
Welbog
I think you're wrong mate, as the array's length will always be equal to arrayMaxValues. I'm not trying to find out it's length and compare it, but if for each index entry, there is an associated value != to ''.
pixelboy
Now I think we've got it, thanks a bunch !!
pixelboy
If you intend on using jQuery syntax, you can use $.each(arr, function(index, value) { // logic here }); too.
Daniel