js 循环**数据结构处理**问题, 遇到瓶颈,求方案(考虑性能)

Anguer
  • 501
  1. 数据结构描述

    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`以上
    ]
  2. 需求描述

    • 计算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'},
          ]
      }
  3. 语言及工具描述

    • JavaScript
    • Node.js
    • Lodash
    • underscore
    • async
    • bluebird
    • 一切你想用的工具都可以
  4. 在线急等...

    • 是时候表演真正的技术了
回复
阅读 1.4k
4 个回答
✓ 已被采纳
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)
    })
})
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]]  )
            }
        }
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]}
];

 function test (users) {
 var obj = {};
 for (var i = 0; i < users.length; i++) {
     var item = users[i].authPlayerIds;
     var id = users[i].id;
     var username = users[i].username;

     for (var j = 0; j < item.length; j++) {
         if (obj[item[j]]){ // 已存在
             obj[item[j]].push({
                 id: id,
                 username: username
             })
         } else {
             obj[item[j]] = [{
                 id: id,
                 username: username
             }]
         }
     }
 }
 console.log(obj);
 }
 test(users);

这应该是最简单的方法了

我在想,为什么把这么多数据放在前端处理

你知道吗?

宣传栏