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)
Author by
Programmer
Updated on July 09, 2022Comments
-
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 over 11 yearsYeah, because it's impossible to use an Object polyfill and pollute the prototype...It's better to be safe.
-
Moritz Roessler over 11 yearsIf 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 -
Denys Séguret over 11 yearsThat'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 over 11 yearsInsanity? Yeah, that's insane!
-
Ian over 11 yearsOther than
Object.defineProperty
, how else can you define a non-enumerable property? (just wondering, I'm not sure) -
Denys Séguret over 11 yearsObject.create but to modify Object's prototype (which I'm not used to) I think you'd have to use defineProperty.
-
Ian over 11 yearsSo I just looked at the polyfill that MDN provides for
Object.keys
, and they don't extend the prototype - they just useObject.keys = function () { //definition };
basically, so it seems like it is non-enumerable. But does that create a newkeys
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 over 11 yearsObject.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 over 11 yearsWell, 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