Sort Array by attribute
Use Array.prototype.sort()
with a custom compare function to do the descending sort first:
champions.sort(function(a, b) { return b.level - a.level }).slice(...
Even nicer with ES6:
champions.sort((a, b) => b.level - a.level).slice(...
Write your own comparison function:
function compare(a,b) {
if (a.level < b.level)
return -1;
if (a.level > b.level)
return 1;
return 0;
}
To use it:
champions.sort(compare).slice(0,3).map(function(champ) {