Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pimoGit
Last active May 9, 2018 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pimoGit/c982b638b60ea4d43b0876a3475f6c8f to your computer and use it in GitHub Desktop.
Save pimoGit/c982b638b60ea4d43b0876a3475f6c8f to your computer and use it in GitHub Desktop.
Get jsons values in a determined order with js
/*
Since you are iterating to an object’s properties, the order of the properties is not so predictable
(in this case it seems to follow the numeric value of the main property string) so you cannot rely on it.
But, if it’s useful to your goal,
you can sort your result by a property you choose.
In this case, I sort the result by the "score_rank" property to retrieve an array of appidS:
*/
var sections = {
"2":{"appid":"appidN1-scorerank5","name":"PLAYERUNKNOWN'S BATTLEGROUNDS","developer":"PUBG Corporation","publisher":"PUBG Corporation","score_rank":5,"positive":277104,"negative":173247,"userscore":60,"owners":22512885,"owners_variance":142499,"players_forever":22385548,"players_forever_variance":142115,"players_2weeks":16894492,"players_2weeks_variance":124193,"average_forever":8570,"average_2weeks":1771,"median_forever":5509,"median_2weeks":1266,"price":"2999"},
"3":{"appid":"appidN2-scorerank2","name":"Counter-Strike: Global Offensive","developer":"Valve","publisher":"Valve","score_rank":2,"positive":2094164,"negative":228950,"userscore":89,"owners":37137816,"owners_variance":180096,"players_forever":35672331,"players_forever_variance":176797,"players_2weeks":9605392,"players_2weeks_variance":94373,"average_forever":17518,"average_2weeks":714,"median_forever":4280,"median_2weeks":282,"price":"1499"},
"4":{"appid":"appidN3-scorerank3","name":"Dota 2","developer":"Valve","publisher":"Valve","score_rank":3,"positive":742208,"negative":101331,"userscore":87,"owners":117932840,"owners_variance":290445,"players_forever":117932840,"players_forever_variance":290445,"players_2weeks":9220909,"players_2weeks_variance":92502,"average_forever":11673,"average_2weeks":1144,"median_forever":258,"median_2weeks":640,"price":"0"},
"1":{"appid":"appidN4-scorerank4","name":"PLAYERUNKNOWN'S BATTLEGROUNDS","developer":"PUBG Corporation","publisher":"PUBG Corporation","score_rank":4,"positive":277104,"negative":173247,"userscore":60,"owners":22512885,"owners_variance":142499,"players_forever":22385548,"players_forever_variance":142115,"players_2weeks":16894492,"players_2weeks_variance":124193,"average_forever":8570,"average_2weeks":1771,"median_forever":5509,"median_2weeks":1266,"price":"2999"},
"6":{"appid":"appidN5-scorerank1","name":"Counter-Strike: Global Offensive","developer":"Valve","publisher":"Valve","score_rank":1,"positive":2094164,"negative":228950,"userscore":89,"owners":37137816,"owners_variance":180096,"players_forever":35672331,"players_forever_variance":176797,"players_2weeks":9605392,"players_2weeks_variance":94373,"average_forever":17518,"average_2weeks":714,"median_forever":4280,"median_2weeks":282,"price":"1499"},
"5":{"appid":"appidN6-scorerank0","name":"Dota 2","developer":"Valve","publisher":"Valve","score_rank":0,"positive":742208,"negative":101331,"userscore":87,"owners":117932840,"owners_variance":290445,"players_forever":117932840,"players_forever_variance":290445,"players_2weeks":9220909,"players_2weeks_variance":92502,"average_forever":11673,"average_2weeks":1144,"median_forever":258,"median_2weeks":640,"price":"0"}
}
var result = Object.values(sections)
.sort(function(a, b) {
return a.score_rank - b.score_rank;
})
.map(function(obj){
return obj.appid;
});
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment