简体   繁体   English

按数组键值对JSON排序

[英]Sort JSON by array key value

I have an example JSON file consisting of: 我有一个包含以下内容的示例JSON文件:

{
    "119996306407030785": {
        "duel": {
            "class": "mage",
            "totalDamage": 64,
            "wins": 5,
            "losses": 2
        },
        "weather": {
            "location": "46544"
        }
    },
    "119333755579138048": {
        "duel": {
            "class": "rogue",
            "totalDamage": 35,
            "losses": 1,
            "wins": 2
        },
        "weather": {
            "location": "95825"
        }
    },
    "112006834713329664": {
        "duel": {
            "totalDamage": 33,
            "losses": 1,
            "wins": 7
        }
    }
}

119996306407030785 is a userID. 119996306407030785是一个用户ID。 I'd like to sort the return by the highest duel.wins of all users for a "top 5" concept. 我想按“前5名”概念中所有用户的最高对决。

The output I would like with the above JSON would be: 我想要上述JSON的输出为:

112006834713329664 - 7 wins   
119996306407030785 - 5 wins  
119333755579138048 - 2 wins 

I've seen multiple questions close to this one while Googling, but can't find any that have JSON similar to my setup. 在谷歌搜索时,我已经看到多个与此问题相关的问题,但是找不到与我的设置类似的JSON问题。

Is this possible considering how my userID's are setup & how random they are? 考虑到我的userID的设置方式和随机性,这是否可行? If so, how can it be done? 如果是这样,怎么办? Please take note that I would not only need the return of the # of wins, but also the userID correlating to that win amount. 请注意,我不仅需要返回获胜次数,还需要与该获胜金额相关的userID。 I would also like to only return the top 5 even if there are dozens of userID's. 即使有许多userID,我也只想返回前5名。

 var data = { "119996306407030785": { "duel": { "class": "mage", "totalDamage": 64, "wins": 5, "losses": 2 }, "weather": { "location": "46544" } }, "119333755579138048": { "duel": { "class": "rogue", "totalDamage": 35, "losses": 1, "wins": 2 }, "weather": { "location": "95825" } }, "112006834713329664": { "duel": { "totalDamage": 33, "losses": 1, "wins": 7 } } } var a = Object.keys(data).map(e => ({id: e, wins: data[e].duel.wins})) .sort((a, b) => b.wins - a.wins); document.write('<pre>' + JSON.stringify(a, 0, 2) + '</pre>') 

To sort objects by duel.wins try this 要按duel.wins对对象进行排序, duel.wins尝试以下操作

function sortByWins(obj) {
    return Object.keys(obj).sort(function(i, j) {
        return obj[i].duel.wins - obj[j].duel.wins;
    }).reduce(function (result, key) {
        result[key] = obj[key];
        return result;
    }, {});
}

Loop over the object using the keys: 使用以下键循环遍历对象:

Object.keys(obj).reduce(function (p, c) {

  // return an array of winner objects
  return p.concat({ id: c, wins: obj[c].duel.wins });
}, []).sort(function (a, b) {

  // sort the objects by wins
  return a.wins < b.wins;
}).forEach(function (el) {

  // Display the information
  console.log([el.id,'-',el.wins,'wins'].join(' '));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM