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;

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.


One thought on “A simple JavaScript trick

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s