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..
-
It's look like a JSON string, you need to parse it first. But before that correct the notation.Sudip– Sudip2013年03月14日 10:47:57 +00:00Commented Mar 14, 2013 at 10:47
-
that's not an array, that's an objectCreynders– Creynders2013年03月14日 10:48:46 +00:00Commented Mar 14, 2013 at 10:48
-
duplicate of stackoverflow.com/questions/126100/…Creynders– Creynders2013年03月14日 10:50:06 +00:00Commented Mar 14, 2013 at 10:50
3 Answers 3
Like this:
Object.keys(linkArray).length
Try this:
var i, length = 0;
for(i in linkArray) {
if(linkArray.hasOwnProperty(i)) {
length++;
}
}
// Here you can use length
1 Comment
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
2 Comments
[]
notation)