数据结构描述varusers=[{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)性能优先(首选)处理结束后的数据结构varauthPlayers={'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一切你想用的工具都可以在线急等...是时候表演真正的技术了
2 回答
潇湘沐
TA贡献1816条经验 获得超6个赞
varauthPlayers={};for(vari=0;ivaruse=users[i]; varuseIds=users[i].authPlayerIds;deleteuse.authPlayerIds;for(varj=0;jif(authPlayers[useIds[j]]==undefined){ authPlayers[useIds[j]]=[];authPlayers[useIds[j]].push(use);}else{authPlayers[useIds[j]].push(use);}//console.log(authPlayers[useIds[j]])}}
大话西游666
TA贡献1817条经验 获得超14个赞
varusers=[{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]},]varret={}users.forEach(function(e){varap=e.authPlayerIdsap.forEach(function(p){vart={}if(ret[p]){t.id=e.id||nullt.username=e.username||null}else{ret[p]=[]t.id=e.id||nullt.username=e.username||null}ret[p].push(t)})})
添加回答
举报
0/150
提交
取消