2 回答
TA贡献1793条经验 获得超6个赞
var authPlayers = {};
for(var i=0;i<users.length;i++){
var use = users[i];
var useIds = users[i].authPlayerIds;
delete use.authPlayerIds;
for(var j=0;j<useIds.length;j++){
if( authPlayers[useIds[j]] == undefined ){
authPlayers[useIds[j]]=[];
authPlayers[useIds[j]].push(use);
}else{
authPlayers[useIds[j]].push(use);
}
//console.log( authPlayers[useIds[j]] )
}
}
TA贡献1817条经验 获得超14个赞
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)
})
})
添加回答
举报