美文网首页
js获取数组中某属性的最大值和最小值

js获取数组中某属性的最大值和最小值

作者: 我是七月 | 来源:发表于2022-11-15 18:53 被阅读0次

一、计算对象数组某属性最大值

export function countMax(arr, key) {
    return Math.max.apply(Math, arr.map(item => { return item[key] }))
}

二、计算对象数组某属性最小值

export function countMin(arr, key) {
    return Math.min.apply(Math, arr.map(item => { return item[key] }))
}

示例

countMax([{name: '张三', age: 53}, {name: '王五', age: 22}], 'age')
countMin([{name: '李四', age: 33}, {name: '赵六', age: 20}], 'age')

相关文章

网友评论

      本文标题:js获取数组中某属性的最大值和最小值

      本文链接:https://www.haomeiwen.com/subject/mdmjxdtx.html