Javascript Array singleOrNone(filter)

Description

Javascript Array singleOrNone(filter)


Array.prototype.singleOrNone = function(filter) {
 var matches = this.filter(filter);
 if (matches.length > 1) {
  throw new Error("Expected 1 but found many");
 }
 if (matches.length == 0) {
  return null;/*w ww.j  av  a  2  s . co m*/
 }

 return matches[0];
}



PreviousNext

Related