Sort Array by attribute

前端 未结 3 1944
太阳男子
太阳男子 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 11:06

    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) {
    

提交回复
热议问题