是否有一个javascript或coffeescript函数(或者可能是下划线 groupBy函数的扩展)接收数组和等价比较器(具有两个参数的布尔函数,而不仅仅是一个参数)作为参数,并根据这个等价对数组元素进行分组比较器?
作为我想要的一个例子:
areEquivalent = (p1, p2) ->
p1.birthYear == p2.birthYear and
p1.birthPlace == p2.birthPlace and
p1.gender == p2.gender
p1 = {name:'Anna', birthYear: 1990, birthPlace: 'Alaska', gender: 'female', hasCar: true, hasChildren: false}
p2 = {name:'John', birthYear: 1990, birthPlace: 'Alaska', gender: 'male', hasCar: true, hasChildren: false}
p3 = {name:'Dora', birthYear: 1980, birthPlace: 'Hawaii', gender: 'female', hasCar: true, hasChildren: true}
p4 = {name:'Lumi', birthYear: 1980, birthPlace: 'Hawaii', gender: 'female', hasCar: false, hasChildren: false}
p5 = {name:'Jack', birthYear: 1990, birthPlace: 'Alaska', gender: 'male', hasCar: false, hasChildren: false}
console.log areEquivalent p1, p2
# false
console.log areEquivalent p3, p4
# true
people = [p1, p2, p3, p4, p5]
console.log _.groupEquivalentObjects(people, areEquivalent)
# [ [p1], [p2,p5], [p3,p4] ]