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.

0

3 Answers 3

23

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;
Sign up to request clarification or add additional context in comments.

Comments

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)

6 Comments

Yeah, because it's impossible to use an Object polyfill and pollute the prototype...It's better to be safe.
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...
Insanity? Yeah, that's insane!
Other than Object.defineProperty, how else can you define a non-enumerable property? (just wondering, I'm not sure)
Object.create but to modify Object's prototype (which I'm not used to) I think you'd have to use defineProperty.
|
-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() );

3 Comments

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
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);
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

Your Answer

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.