zoukankan      html  css  js  c++  java
  • 取数组最大/最小值(数组中对象某元素最大值)

    通过 Math.min.apply和Math.max.apply取出最大值和最小值

    方法利用Math的min和max方法;可返回指定的数字中带有最低值的数字或最大值的数字

    但是在min和max里不能传入数组,

    所以结合apply方法;进行对数组取最大值和最小值。

    apply是调用一个对象的一个方法,用另一个对象替换当前对象其参数为:apply(Function, Args),

    apply可以最多可以接受两个参数,第一个参数为新的this对象,第二个参数必须是数组。

    Math.max.apply(null, arr)
    可以看做是
    apply(Math.max, arr)
    然后,arr是一个参数列表,对于max方法,其参数是若干个数,即
    Math.max(a, b, c, d, ...)
    当使用apply时,把所有参数加入到一个数组中,即
    arr = [a, b, c, d, ...]
    代入到原式,
    Math.max.apply(null, [a, b, c, d, ...])
    实际上等同于
    Math.max(a, b, c, d, ...)

    var arr = [{
    'name': 'abc',
    'age': 20
    },
    {
    'name': 'cde',
    'age': 19
    },
    {
    'name': 'dfc',
    'age': 25
    },
    {
    'name': 'bde',
    'age': 21
    },
    ];

    let min = Math.min.apply(Math, arr .map(function (o) {
    return o.age
    }
    ))

    console.log(min)

    let max = Math.max.apply(Math, arr .map(function (o) {
    return o.age
    }
    ))
    console.log(max)
  • 相关阅读:
    webapi帮助文档swagger
    后台任务hangfire
    EF6 DbModelBuilder
    .net跨平台分析软件
    EF迁移命令
    NuGet Package Explorer
    ghost blog 中文资料
    IDependency自动注册autofac
    Abp zero 示例运行
    AutoMapperExtension
  • 原文地址:https://www.cnblogs.com/yutianA/p/10511548.html
Copyright © 2011-2022 走看看