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..

asked Mar 14, 2013 at 10:45
3
  • It's look like a JSON string, you need to parse it first. But before that correct the notation. Commented Mar 14, 2013 at 10:47
  • that's not an array, that's an object Commented Mar 14, 2013 at 10:48
  • duplicate of stackoverflow.com/questions/126100/… Commented Mar 14, 2013 at 10:50

3 Answers 3

4

Like this:

Object.keys(linkArray).length
answered Mar 14, 2013 at 10:49

2 Comments

Will not work in IE8 since it has no support for Object.keys
Yeah, but that's a topic for a different question IMO.
1

Try this:

var i, length = 0;
for(i in linkArray) {
 if(linkArray.hasOwnProperty(i)) {
 length++;
 }
}
// Here you can use length
answered Mar 14, 2013 at 10:50

1 Comment

@Amrenda I checked, it gives 13 as expected http://jsbin.com/ubelen/1/edit
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

answered Mar 14, 2013 at 10:50

2 Comments

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".
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)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.