Sort Array by attribute

前端 未结 3 1960
太阳男子
太阳男子 2020-12-24 10:31

I right now just get the first 3 Object of an Array and map over them:

    { champions.slice(0,3).map(function(ch
3条回答
  •  感情败类
    2020-12-24 10:58

    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(...
    

提交回复
热议问题