组由嵌套JSON和对象
问题描述:
的阵列之间的属性和值的总和
我有用户阵列他们的姓名,组由嵌套JSON和对象
var users = [{'name':'zulekha'}, {'name':'deepika'}];
我取由从JIRA的API的每个用户worklogged。所以我得到这样的对象。
var worklogResult = {
"issues": [
{
"fields": {
"worklog": {
"worklogs": [
{
"author": {
"name": "zulekha",
},
"timeSpentSeconds": 180
},
{
"author": {
"name": "deepika",
},
"timeSpentSeconds": 210
}
]
}
}
},
{
"fields": {
"worklog": {
"worklogs": [
{
"author": {
"name": "deepika",
},
"timeSpentSeconds": 140
}
]
}
}
},
{
"fields": {
"worklog": {
"worklogs": [
{
"author": {
"name": "zulekha",
},
"timeSpentSeconds": 600,
}
]
}
}
},
{
"fields": {
"worklog": {
"worklogs": []
}
}
}
]
}
现在我想以这样的方式,我可以得到下面的输出匹配worklogResult与用户。
输出= [{ '名': 'zulekha', 'timeSpentSeconds':780},{ '名': '迪皮卡', 'timeSpentSeconds':350}]
任何人都可以建议我如何实现这个?
答
使用到平坦嵌套对象
_.chain(worklogResult.issues)
.flatMap('fields.worklog.worklogs')
.thru(function(spents) {
return _.map(users, function(user) {
return _.merge(user, {
timeSpentSeconds: _.chain(spents)
.filter(['author.name', user.name])
.map('timeSpentSeconds')
.sum()
.value()
})
})
})
.value()
感谢。它有助于 :) – zulekha