Last active
August 29, 2015 14:25
-
-
Save ebta/9ce83bfb7d6950c4811c to your computer and use it in GitHub Desktop.
Length of a JavaScript object (associative array)
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
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