0

Here is my array

var linkArray = {
boothsizeDiv_link: false,
furnishingsprovidedDiv_link: false,
electricalDiv_link: false,
rentalfurnishingsDiv_link: false,
gesgraphicsDiv_link: false,
geslaborDiv_link: false,
contractorDiv_link: false,
carpetingDiv_link: false,
boothlightingDiv_link: false,
javitsDiv_link: false,
boothsealDiv_link: false,
mannequinsDiv_link: false,
calcDiv_link: false
};

How to find out this array length? I have googled it but no use..

Vamshi
  • 186
  • 4
  • 16

3 Answers3

4

Like this:

Object.keys(linkArray).length
elclanrs
  • 92,861
  • 21
  • 134
  • 171
1

Try this:

var i, length = 0;
for(i in linkArray) {
  if(linkArray.hasOwnProperty(i)) {
    length++;
  }
}

// Here you can use length
Vadim
  • 8,701
  • 4
  • 43
  • 50
  • @Amrenda I checked, it gives 13 as expected [http://jsbin.com/ubelen/1/edit](http://jsbin.com/ubelen/1/edit) – Vadim Mar 14 '13 at 11:05
0

The solution would be

Object.getOwnPropertyNames(linkArray).length

but be careful, because this is NOT an array, this is a object.

And beware that it will not work on Internet Explorers below 9.

In this MDN Article you can see how getOwnPropertyNames can be used.

If you want to use it also in Browser that don't support it, you just insert the following snippet in your script, at the beginning:

Object.getOwnPropertyNames = Object.getOwnPropertyNames || function(obj) {
    var ownProperties = [];
    var current = '';
    for(current in obj) {
      if(linkArray.hasOwnProperty(current)) {
        ownProperties.push(current);
      }
    }
    return ownProperties;
}

(I just wrote it, and can't test it at the moment, but it should work)

with this snippet you simulate Object.getOwnPropertyNames in browsers that don't have it natively.

And clearly instead of getOwnPropertyNames you can also use keys

ramsesoriginal
  • 1,860
  • 1
  • 13
  • 16
  • Array is also and Object in JavaScript. The above will not work because length property is defined for an Array and some other objects in JS but not for "Object". – Alok Swain Mar 14 '13 at 10:54
  • Yes, sorry, i pressed "post" prematurely. Editetd to correct it. And yes, every array is an object, but not every object is an array (although you can access the members through the `[]` notation) – ramsesoriginal Mar 14 '13 at 10:58