如何将此过滤器的值映射到新数组。我的过滤器工作正常,但最终结果需要具有数组im过滤器上的一个属性。我正在使用ramda。
let bugs = bugsfromBugzilla();
let users = utils.ReadJsonFile('./team.json');
let data = R.filter(bug => {
return R.any(filter => {
return R.contains(filter.assignee, bug.assigned_to);
}, users);
}, bugs);
用户数组如下:
[
{
"assignee": "jesse@email.com",
"id": 843568062370470
},
{
"assignee": "Ryan@email.com",
"id": 848490202942168
}
]
我需要id才能成为数据变量的一部分。如果我只能在新数组中映射想要的属性,那也很好。
答案 0 :(得分:1)
在没有示例输入或输出数据的情况下,这里仍然需要什么尚不清楚。 (好的,我们有示例用户,但没有示例错误。)
pipe
不过,我认为这可能很接近:
const {contains, pipe, filter, any, project, map, assoc, prop, find} = R
const checkMatch = bug => user => contains(user.assignee, bug.assigned_to)
const extract = (users) => pipe(
filter(bug => any(checkMatch(bug), users)),
project(['bug_id', 'assigned_to', 'bar']),
map(bug => assoc('user_id', prop('id', find(checkMatch(bug), users)), bug)),
)
const bugs = [
{bug_id: 'abc', assigned_to: "jesse@email.com", foo: 1, bar: 42, baz: 'z'},
{bug_id: 'def', assigned_to: "fred@flintstone.com", foo: 2, bar: 43, baz: 'y'},
{bug_id: 'ghi', assigned_to: "jesse@email.com", foo: 3, bar: 44, baz: 'x'},
{bug_id: 'jkl', assigned_to: "Ryan@email.com", foo: 4, bar: 45, baz: 'w'},
{bug_id: 'mno', assigned_to: "Mr. Ryan <Ryan@email.com>", foo: 5, bar: 46, baz: 'v'},
{bug_id: 'pqr', assigned_to: "wilma@flintstone.com", foo: 6, bar: 47, baz: 'u'}
]
const users = [
{"assignee": "jesse@email.com", "id": 843568062370470},
{"assignee": "Ryan@email.com", "id": 848490202942168}
]
console.log(extract(users)(bugs))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
请注意,由于对问题的评论/回复之间存在延迟,因此我在这里猜测您为什么需要contains
。我猜想您可能有一个像Mr. Ryan <Ryan@email.com>
这样的值需要匹配Ryan@email.com
。如果您有完全匹配的内容,我会写:
const checkMatch = bug => user => user.assignee === bug.assigned_to
reduce
通常,我发现这样的解决方案(将步骤组合在一起)比基于reduce
的解决方案更干净。但是,由于这些步骤都相当复杂,而且我已经重用了在过滤中找到的用户来更新对象,因此我尝试使用reduce
进行比较。我实际上更喜欢它:
const {reduce, find, contains, append, assoc, pick} = R
const extract = (users) => reduce((all, bug) => {
const user = find( user => contains(user.assignee, bug.assigned_to), users);
return user
? append(assoc('user_id', user.id, pick(['bug_id', 'assigned_to', 'bar'], bug)), all)
: all
}, [])
const bugs =[{"assigned_to": "jesse@email.com", "bar": 42, "baz": "z", "bug_id": "abc", "foo": 1}, {"assigned_to": "fred@flintstone.com", "bar": 43, "baz": "y", "bug_id": "def", "foo": 2}, {"assigned_to": "jesse@email.com", "bar": 44, "baz": "x", "bug_id": "ghi", "foo": 3}, {"assigned_to": "Ryan@email.com", "bar": 45, "baz": "w", "bug_id": "jkl", "foo": 4}, {"assigned_to": "Mr. Ryan <Ryan@email.com>", "bar": 46, "baz": "v", "bug_id": "mno", "foo": 5}, {"assigned_to": "wilma@flintstone.com", "bar": 47, "baz": "u", "bug_id": "pqr", "foo": 6}]
const users = [{"assignee": "jesse@email.com", "id": 843568062370470}, {"assignee": "Ryan@email.com", "id": 848490202942168}]
console.log(extract(users)(bugs))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
(同样,如果数据支持,我会选择user.assignee === bug.assigned_to
而不是contains
。)