syropian
8/29/2013 - 7:53 PM

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.

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;
}