7

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.

Earth
  • 3,477
  • 6
  • 37
  • 78

3 Answers3

22

You could use Object.keys(animals).length

Or

var count = 0;
for (var animal in animals) {
    if (animals.hasOwnProperty(animal)) {
        count++;
    }
}
// `count` now holds the number of object literals in the `animals` variable

Or one of many jQuery solutions that may or may not be the most efficient:

var count = $.map(animals, function(n, i) { return i; }).length;
Ian
  • 50,146
  • 13
  • 101
  • 111
1

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 :

var n = 0;
for (var _ in animals) n++;

(as it is an object literal, no need for hasOwnProperty)

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Yeah, because it's impossible to use an Object polyfill and pollute the prototype...It's better to be safe. – Ian Dec 10 '12 at 14:41
  • That's not impossible but your code shouldn't guard against insanity. Anybody sane adding a property to Object's prototype would at least make it non enumerable... – Denys Séguret Dec 10 '12 at 14:52
  • Insanity? Yeah, that's insane! – Ian Dec 10 '12 at 14:55
  • Other than `Object.defineProperty`, how else can you define a non-enumerable property? (just wondering, I'm not sure) – Ian Dec 10 '12 at 14:56
  • Object.create but to modify Object's prototype (which I'm not used to) I think you'd have to use defineProperty. – Denys Séguret Dec 10 '12 at 14:59
  • So I just looked at the polyfill that MDN provides for `Object.keys`, and they don't extend the prototype - they just use `Object.keys = function () { //definition };` basically, so it seems like it is non-enumerable. But does that create a new `keys` method for every string constructed instead of sharing a general prototype method? I would guess they are doing that in order to avoid this enumerable problem, even if it takes a minor hit? – Ian Dec 10 '12 at 15:04
-1

Can't you use an array?

Anyway, in an object, you can do that:

Object.prototype.length = function() {
    var count = 0, k=null;
    for (k in this) {
        if (this.hasOwnProperty(k)) count++;
    }
    return count;
}
console.log( animals.length() );
neiker
  • 8,887
  • 4
  • 29
  • 32
  • 3
    If he needs to name his properties he can't use an Array, and modifying the Objects prototype is not considered a good practice, especially if theres an `Object`s method which you can use for this -> `Object.keys` gives you an array with the key names which then has a length propertie – Moritz Roessler Dec 10 '12 at 14:46
  • Object.keys is EcmaScript 5 standard, don't works on IE8 or lt. Anyway you CAN use Array and name your properties: var things = []; things['someShit'] = 'Hello'; things['someShit2'] = 'World!'; console.log(things.someShit+' '+things.someShit2); – neiker Dec 13 '12 at 15:02
  • 1
    Well, giving arrays named keys is a bad idea either, because it breaks some off the arrays methods like slice or forEach (which simply would ignore the named keys) as well you would have to use Object.keys(). Length or iterating with keys in to get the right length, just to name a few, her is a job in http://jsbin.com/anixap/1/edit Sry for the spelling, I'm on mobile – Moritz Roessler Dec 14 '12 at 13:44