美文网首页
es6数组取最大值

es6数组取最大值

作者: 子语喵 | 来源:发表于2020-07-17 15:11 被阅读0次

怎样取数组最大值


let arr = [1, 2, 3, 4, 5, 6];
//ES5:方法
let maxNum5 = Math.max.apply(null, arr);
console.log(maxNum5) // 6
// ES6:方法
let maxNum6 = Math.max(...arr);
console.log(maxNum6) // 6

怎样去数组对象里面的最大值

let obj = [{
    age: 12
}, {
    age: 24
}, {
    age: 48
}, ]
//利用map遍历。return出数组
let arrs = obj.map((value, index, array) => {
    return value.age
})
console.log(Math.max(...arrs)) // 48

相关文章

网友评论

      本文标题:es6数组取最大值

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