Sort Array by attribute

前端 未结 3 1959
太阳男子
太阳男子 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条回答
  •  -上瘾入骨i
    2020-12-24 11:16

    The pure JS solutions are nice. But if your project is set up via npm, you can also use Lodash or Underscore. In many cases those are already sub-dependencies so no extra weight is incurred.

    Combining ES6 and _.orderBy provided by lodash

    _.orderBy(champions, [c => c.level], ['desc']).slice(0,3)
    

    This is a powerful little utility. You can provide multiple tie-breaking sort keys to orderBy, and specify an order for each individually.

提交回复
热议问题