To count the number of objects in object literal using Jquery

23,551

Solution 1

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;

Solution 2

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)

Share:
23,551
Programmer
Author by

Programmer

Updated on July 09, 2022

Comments

  • Programmer
    Programmer almost 2 years

    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.

  • Ian
    Ian over 11 years
    Yeah, because it's impossible to use an Object polyfill and pollute the prototype...It's better to be safe.
  • Moritz Roessler
    Moritz Roessler over 11 years
    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 Objects method which you can use for this -> Object.keys gives you an array with the key names which then has a length propertie
  • Denys Séguret
    Denys Séguret over 11 years
    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...
  • Ian
    Ian over 11 years
    Insanity? Yeah, that's insane!
  • Ian
    Ian over 11 years
    Other than Object.defineProperty, how else can you define a non-enumerable property? (just wondering, I'm not sure)
  • Denys Séguret
    Denys Séguret over 11 years
    Object.create but to modify Object's prototype (which I'm not used to) I think you'd have to use defineProperty.
  • Ian
    Ian over 11 years
    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?
  • neiker
    neiker over 11 years
    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);
  • Moritz Roessler
    Moritz Roessler over 11 years
    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 jsbin.com/anixap/1/edit Sry for the spelling, I'm on mobile