Skip to content

Instantly share code, notes, and snippets.

@gfcarvalho
Created March 25, 2014 20:57
Show Gist options
  • Save gfcarvalho/9771165 to your computer and use it in GitHub Desktop.
Save gfcarvalho/9771165 to your computer and use it in GitHub Desktop.
Count the number of properties of an Object, in JavaScript.
function countProperties(obj) {
var count = 0,
k;
for (k in obj) {
if (obj.hasOwnProperty(k)) {
count += 1;
}
}
return count;
}
@gfcarvalho
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment