使用下划线弹出数组对象的流星集合

时间:2014-10-20 22:56:46

标签: meteor underscore.js

是否可以在数组对象上使用下划线,而不仅仅是属性名称

这可以正常工作,因为它的属性名称只是一个字符串ID值。

return Collaborators.find({collaboratorId: {$in: _.pluck(topics, '_id')}});

但是如果我需要使用像这样的数组

Meteor.users.find({_id: {$in: _.pluck(topics, 'collaboratorsIds')}});

是CollaboratorsIds是

 [ 'btcTd637xvDrgTieJ', 'DLqCAnw7CSakRTy6S', 'btcTd637xvDrgTieJ' ]  

1 个答案:

答案 0 :(得分:2)

给出一系列主题,如

var topics = [
  {
    _id: "...",
    title: "...",
    collaboratorsIds: ["qwerty", "asdfg", "zxcvbn"],
    ...
  },
  {
    _id: "...",
    title: "...",
    collaboratorsIds: ["yuiop", "hjkl;"],
    ...
  },
  {
    _id: "...",
    title: "...",
    collaboratorsIds: ["qazwsx"],
    ...
  }
]

调用_.pluck(topics, "collaboratorsIds")将为您提供一系列数组:

[["qwerty", "asdfg", "zxcvbn"], ["yuiop", "hjkl;"], ["qazwsx"]]

您可以使用_.flatten(..., true)将其展平为一个简单的数组:

_.flatten(_.pluck(topics, "collaboratorsIds"), true)
  -> ["qwerty", "asdfg", "zxcvbn", "yuiop", "hjkl;", "qazwsx"]