Created
March 25, 2014 20:57
-
-
Save gfcarvalho/9771165 to your computer and use it in GitHub Desktop.
Count the number of properties of an Object, in JavaScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countProperties(obj) { | |
var count = 0, | |
k; | |
for (k in obj) { | |
if (obj.hasOwnProperty(k)) { | |
count += 1; | |
} | |
} | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you are using ES5, you can use:
Object.keys(obj).lenght;
.There's also a polyfill for
Objects.keys
in the following link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys