Javascript Array toUniqueDictionary(key)

Description

Javascript Array toUniqueDictionary(key)


Array.prototype.toUniqueDictionary = function(key) {
 if (!key)/* ww w. j  a v  a2s .  co m*/
  key = function(x) { return x.id; };

 var dictionary = {};
 this.forEach(function(item) {
  var currentKey = key(item);
  if (!currentKey)
   return true;
  dictionary[currentKey] = item;
 });
 return dictionary;
};



PreviousNext

Related