If I have a JavaScript object, say
var myObject = new Object();
myObject["firstname"] = "Gareth";
myObject["lastname"] = "Simpson";
myObject["age"] = 21;
is there a built-in or accepted best practice way to get the length of this object?
If I have a JavaScript object, say
var myObject = new Object();
myObject["firstname"] = "Gareth";
myObject["lastname"] = "Simpson";
myObject["age"] = 21;
is there a built-in or accepted best practice way to get the length of this object?
If you don't care about supporting Internet Explorer 8 or lower, you can easily get the number of properties in an object by applying the following two steps:
Object.keys()
to get an array that contains the names of only those properties that are enumerable orObject.getOwnPropertyNames()
if you want to also include the names of properties that are not enumerable..length
property of that array.If you need to do this more than once, you could wrap this logic in a function:
How to use this particular function:
See also this Fiddle for a demo.
You can simply use
Object.keys(obj).length
on any object to get its length. Object.keys returns an array containing all of the object keys (properties) which can come in handy for finding the length of that object using the length of the corresponding array. You can even write a function for this. Let's get creative and write a method for it as well (along with a more convienient getter property):Here is a completely different solution that will only work in more modern browsers (IE9+, Chrome, Firefox 4+, Opera 11.60+, Safari 5.1+)
See jsFiddle
Setup your Associative Array class
Now you can use this code as follows...
The most robust answer (i.e. that captures the intent of what you're trying to do while causing the fewest bugs) would be:
There's a sort of convention in JavaScript that you don't add things to Object.prototype, because it can break enumerations in various libraries. Adding methods to Object is usually safe, though.
Here's an update as of 2016 and widespread deployment of ES5 and beyond. For IE9+ and all other modern ES5+ capable browsers, you can use
Object.keys()
so the above code just becomes:This doesn't have to modify any existing prototype since
Object.keys()
is now built in.Edit: Objects can have symbolic properties which can not be returned via Object.key method. So the answer would be incomplete without mentioning them.
Symbol type was added to the language to create unique identifiers for object properties. Main benefit of Symbol type is prevention of overwrites.
Object.keys
orObject.getOwnPropertyNames
does not work for symbolic properties. To return them you need to useObject.getOwnPropertySymbols
.To not mess with the prototype or other code, you could build and extend your own object:
If you always use the add method, the length property will be correct. If you're worried that you or others forget about using it, you could add the property counter which the others have posted to the length method, too.
Of course, you could always overwrite the methods. But even if you do, your code would probably fail noticeably, making it easy to debug. ;)
This works for me: