如何获取同一文档中存在于两个不同数组中的元素。
例如,在帖子集合中,文档具有字段“interestbycreator”和“interestbyreader”。每个字段都包含用户ID。
'interestbycreator': //an array of ids here. IdA, idB, IdC, IdD, IdE,
'interestbyreader': //an array of ids here. IdB, idE, iDF
基本上,我希望找到两个数组中都存在的所有id,这应该是IdB和IdE。
我能够使用underscore从数组中拔出所有值并将它们存储在一个变量中。可以用这种方式将它们彼此比较并返回重复项吗?或者有其他解决方案吗?
示例:检索'interestbyreader'中的所有ID。
var interestbypostcreater = Posts.find({_id: Meteor.user().profile.postcreated[0]}, {fields: {interestbyreader: 1}}).fetch();
var interestedReaderIds = _.chain(interestbypostcreator).pluck('interestbyreader').flatten().value();
假设我有另一个存储在变量interestIdcreator中的数组'interestbycreator',它们可以进行比较以查找重复项并返回这些重复项吗?
_.intersection
吗?http://underscorejs.org/#intersection - saimeunt