亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

js 循環**數據結構處理**問題, 遇到瓶頸,求方案(考慮性能)

js 循環**數據結構處理**問題, 遇到瓶頸,求方案(考慮性能)

數據結構描述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一切你想用的工具都可以在線急等...是時候表演真正的技術了
查看完整描述

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]]  )

            }

        }


查看完整回答
反對 回復 2018-10-29
?
大話西游666

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)

    })

})


查看完整回答
反對 回復 2018-10-29
  • 2 回答
  • 0 關注
  • 1009 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號