Skip to content

Instantly share code, notes, and snippets.

@ebta
Last active August 29, 2015 14:25
Show Gist options
  • Save ebta/9ce83bfb7d6950c4811c to your computer and use it in GitHub Desktop.
Save ebta/9ce83bfb7d6950c4811c to your computer and use it in GitHub Desktop.
Length of a JavaScript object (associative array)
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
// Get the size of an object
var size = Object.size(myArray);
// Simple methods, maybe not working in IE8
Object.keys(myArray).length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment