Code:
var animals = {
"elephant": {
"name" : "Bingo",
"age" : "4"
},
"Lion": {
"name" : "Tango",
"age" : "8"
},
"Tiger": {
"name" : "Zango",
"age" : "7"
}
}
I want to count the number of objects using Jquery in this object literal.
You could use
Object.keys(animals).length
Or
Or one of many jQuery solutions that may or may not be the most efficient:
Can't you use an array?
Anyway, in an object, you can do that:
If you want something cross browser, that is also working on IE8, you can't do it in a really clean way (see compatibility of the keys property).
I suggest this :
(as it is an object literal, no need for hasOwnProperty)