【发布时间】:2018-02-06 09:25:26
【问题描述】:
我正在研究 lodash,我需要从数组的大集合中过滤数据。数组的集合就是这种类型的数据:
[ { poll_options: [ [Object] ],
responder_ids: [ '5a7189c14615db31ecd54347', '59ffb41f62d346204e0a199b' ],
voted_ids: [ '5a7189c14615db31ecd54347' ],
_id: 5a7878833a1bf4238ddc5cef },
{ poll_options: [ [Object] ],
responder_ids: [ '5a7189c14615db31ecd54347' ],
voted_ids: [ '5a7189c14615db31ecd54347' ],
_id: 5a787756217db51698ea8fd6 } ]
我想过滤其中包含不在 voted_ids 中的 ids 值的数组(即对于第一个对象,它应该返回这个 59ffb41f62d346204e0a199b nd 对于第二个集合,它应该返回空数组)这意味着我必须只返回那些不在 voted_ids 上但在 responder_ids 上的值。我的代码是这样的
_.map(polls,(poll) => {
// console.log(poll.responder_ids)
return _.filter(poll.responder_ids,(responder) => {
return _.find(poll.voted_ids,(voted) => {
return !_.includes(responder,voted)
})
但它不返回过滤后的数组,而是返回整个集合。我做错了什么??
现在它的返回结果是这样的......
[ [ '59ffb41f62d346204e0a199b' ], [] ]
我想要单个数组而不是多数组。
【问题讨论】:
-
_.filter总是返回一个真实的值,你可能想要_.some代替。 -
而不是
_.map使用_.flatMap
标签: javascript arrays lodash