Skip to content

Instantly share code, notes, and snippets.

@syropian
Created August 29, 2013 19:53
Show Gist options
  • Save syropian/6382640 to your computer and use it in GitHub Desktop.
Save syropian/6382640 to your computer and use it in GitHub Desktop.
A few handy JS helper methods for getting the count of a JS object, as well as getting the index of an object by key.
//Helper Methods
function getObjectCount(obj) {
var count = 0;
var key;
for(key in obj) {
if(obj.hasOwnProperty(key)) count++;
}
return count;
}
function getIndexOfObjectKey(obj, attr) {
var i = 0;
$.each(obj, function(k, v){
if(k === attr) return false;
i++;
});
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment