js对数组排序

 

list: [
{ id: 1, name: 'jack' },
{ id: 2, name: 'may' },
{ id: 3, name: 'shawn' },
{ id: 4, name: 'tony' },
]

1.Math方法

// 最大值 4
Math.max.apply(Math,this.list.map(item => { return item.id }))

// 最小值 1
Math.min.apply(Math,this.list.map(item => { return item.id }))

2.

sort排序

需要注意的是,sort()对数组排序,不开辟新的内存,对原有数组元素进行调换, 所以这种操作会使得原来的数组元素的位置发生变化.

并且在immer.js的produce中,这玩意不能使用.

// 最大值 4
this.list.sort((a, b) => { return b-a })[0].id

// 最小值 1
this.list.sort((a, b) => { return a-b })[0].id

 

原作者:

https://blog.csdn.net/qq285679784/article/details/115951200

posted @   古德拉克  阅读(511)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
点击右上角即可分享
微信分享提示