数据结构描述var users = [ {id: 1, username: '123', authPlayerIds: [1001, 1002, 1003]}, {id: 2, username: 'qwe', authPlayerIds: [1003, 1004, 1005]}, {id: 3, username: 'fdf', authPlayerIds: [1002, 1007, 1088]}, {id: 4, username: 'dsa', authPlayerIds: [1001, 1022, 1033]}, ... // 注意: 数据在`20000`以上]需求描述计算authPlayerIds字段中的每一项(id)所出现的次数以及包含它的用户(user)性能优先(首选)处理结束后的数据结构var authPlayers = { '1001': [ {id: 1, username: '123'}, {id: 4, username: 'dsa'} ], '1002': [ {id: 1, username: '123'}, {id: 3, username: 'fdf'}, {id: 4, username: 'dsa'}, ], '1003': [ {id: 1, username: '123'}, {id: 2, username: 'qwe'}, ]}语言及工具描述JavaScriptNode.jsLodashunderscoreasyncbluebird一切你想用的工具都可以在线急等...是时候表演真正的技术了
1 回答
汪汪一只猫
TA贡献1898条经验 获得超8个赞
var users = [
{id: 1, username: '123', authPlayerIds: [1001, 1002, 1003]},
{id: 2, username: 'qwe', authPlayerIds: [1003, 1004, 1005]},
{id: 3, username: 'fdf', authPlayerIds: [1002, 1007, 1088]},
{id: 4, username: 'dsa', authPlayerIds: [1001, 1022, 1033]},
]
var ret = {}
users.forEach(function(e){
var ap = e.authPlayerIds
ap.forEach(function(p){
var t = {}
if (ret[p]) {
t.id = e.id || null
t.username = e.username || null
} else {
ret[p] = []
t.id = e.id || null
t.username = e.username || null
}
ret[p].push(t)
})
})
添加回答
举报
0/150
提交
取消