Blog Stats

A simple JavaScript trick

Given a JSON array, or any JavaScript object array, how do I find out the size or the length of the array without a length or size property? This is actually a very simple to solve. Just use for ( x in y) statement and use a counter variable to increment the count while you process through the data in the array. Here is one example:

var count = 0;
for (x in array){
array[x] = 0;
count++;
}

If all you do with the length/size information is to traverse through the array in for (int x=0; x<arraySize;x++), then you can just use the for ( x in y) statement without the extra counter variable. If you have a simpler trick or an obvious fact to share, just leave me a comment.