pixelboy pixelboy - 6 months ago 9
Javascript Question

Trying to find if array is full or not

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.

Answer

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;
}