基于对象属性值的对象数组的交集

时间:2013-03-15 20:37:09

标签: coffeescript underscore.js

array1 = [ { id: "a", score: 1 }, { id: "b", score: 3 }, { id: "c", score: 8 }]
array2 = [ { id: "a", score: 2 }, { id: "z", score: 5 }, { id: "c", score: 1 }]
array3 = [ { id: "a", score: 3 }, { id: "f", score: 2 }, { id: "c", score: 2 }]

如何获得仅包含具有id属性值的对象的结果数组,该属性值在所有数组中都相同?即:

resultyArray = [ 
  { id: "a", score: 1 }, 
  { id: "a", score: 2 }, 
  { id: "a", score: 3 },
  { id: "c", score: 8 },
  { id: "c", score: 1 },
  { id: "c", score: 2 }
]

1 个答案:

答案 0 :(得分:1)

让我们说每个数组上出现id的对象“无处不在”(命名事情很难= P):

filterUbiquitousObjects = (arrays...) ->
  objectsById = {}
  (objectsById[obj.id] or= []).push obj for obj in arr for arr in arrays 
  [].concat (arr for id, arr of objectsById when arr.length is arrays.length)...

array1 = [{ id: "a", score: 1 }, { id: "b", score: 3 }, { id: "c", score: 8 }]
array2 = [{ id: "a", score: 2 }, { id: "z", score: 5 }, { id: "c", score: 1 }]
array3 = [{ id: "a", score: 3 }, { id: "f", score: 2 }, { id: "c", score: 2 }]

console.log filterUbiquitousObjects array1, array2, array3
# Output:
# [{id:"a", score:1}, {id:"a", score:2}, {id:"a", score:3}, {id:"c", score:8}, 
# {id:"c", score:1}, {id:"c", score:2}]

嵌套的for循环有点难看,但这应该是O(n)(n是对象的总数),除非我搞砸了。

更新:你也可以这样做:

res = []
for id, arr of objectsById when arr.length is arrays.length
  res = res.concat arr
res

而不是神秘的[].concat (arr for id, arr of objectsById when arr.length is arrays.length)...行。它可以说更具可读性,并且会产生更好的JS:)

相关问题