我正在寻找一个非常快速,干净和有效的方法来获得以下JSON切片中的最大“y”值:

[
  {
    "x": "8/11/2009",
    "y": 0.026572007
  },
  {
    "x": "8/12/2009",
    "y": 0.025057454
  },
  {
    "x": "8/13/2009",
    "y": 0.024530916
  },
  {
    "x": "8/14/2009",
    "y": 0.031004457
  }
]

for循环是唯一的方法吗?我很喜欢用Math.max。


当前回答

首先,你应该解析JSON字符串,这样你就可以很容易地访问它的成员:

var arr = $.parseJSON(str);

使用map方法提取值:

arr = $.map(arr, function(o){ return o.y; });

然后你可以在max方法中使用数组:

var highest = Math.max.apply(this,arr);

或者作为一行语句:

var highest = Math.max.apply(this,$.map($.parseJSON(str), function(o){ return o.y; }));

其他回答

在对象数组中找到属性“Y”值最大的对象

一种方法是使用Array reduce..

const max = data.reduce(function(prev, current) {
    return (prev.y > current.y) ? prev : current
}) //returns object

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce http://caniuse.com/#search=reduce (IE9及以上版本)

如果你不需要支持IE(只支持Edge),或者可以使用预编译器,比如Babel,你可以使用更简洁的语法。

const max = data.reduce((prev, current) => (prev.y > current.y) ? prev : current)
// Here is very simple way to go:

// Your DataSet.

let numberArray = [
  {
    "x": "8/11/2009",
    "y": 0.026572007
  },
  {
    "x": "8/12/2009",
    "y": 0.025057454
  },
  {
    "x": "8/13/2009",
    "y": 0.024530916
  },
  {
    "x": "8/14/2009",
    "y": 0.031004457
  }
]

// 1. First create Array, containing all the value of Y
let result = numberArray.map((y) => y)
console.log(result) // >> [0.026572007,0.025057454,0.024530916,0.031004457]

// 2.
let maxValue = Math.max.apply(null, result)
console.log(maxValue) // >> 0.031004457

比较三个处理负号大小写的oneliner(在数组中输入):

var maxA = a.reduce((a,b)=>a.y>b.y?a:b).y; // 30 chars time complexity:  O(n)

var maxB = a.sort((a,b)=>b.y-a.y)[0].y;    // 27 chars time complexity:  O(nlogn)
           
var maxC = Math.max(...a.map(o=>o.y));     // 26 chars time complexity: >O(2n)

这里是可编辑的示例。来自:maxA, maxB和maxC的想法(maxB的副作用是数组a被改变,因为排序是到位的)。

var a = [ {" x ": 8/11/2009”、“y”:026572007 {" x "}表示:“8/12/2009”、“y”:025057454} {" x ": 8/14/2009”、“y”:031004457 {" x "}表示:“8/13/2009”、“y”:024530916} ] var maxA = a.reduce((a,b)=>a.y>b.y?a:b) y; var maxC = Math.max(.. a.map(o=>o.y)); var maxB = a.sort((a,b)=>b.y-a.y)[0].y; 文件。身体。innerHTML = ' < pre - > maxA: $ {maxA} \ nmaxB: $ {maxB} nmaxC: $ {maxC} < / pre - > ';

对于较大的数组,Math.max…最大调用堆栈大小超过(Chrome 76.0.3809, Safari 12.1.2,日期2019-09-13)

let a =数组(400*400)。Fill ({"x": "8/11/2009", "y": 0.026572007}); // Exception:超过最大调用堆栈大小 尝试{ let max1= Math.max。apply(Math, a.p ap(o => o.y)); } catch(e) {console.error('Math.max. error ')应用:',e.message)} 尝试{ let max2= Math.max(…a.map(o=>o.y)); } catch(e) {console.error('Math. error ')Max-map:', e.message)}

4元素数组的基准测试

ES6解决方案

Math.max(…array.map(函数(o){返回o.y;}))

详情见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

对公认答案的解释和更一般化的方法

如果有人在这里找到所有这些键的最大值(一种广义的方式):

const temp1 = [ { "name": "Month 8 . Week 1", "CATEGORY, Id 0": null, "CATEGORY, Id 1": 30.666666666666668, "CATEGORY, Id 2": 17.333333333333332, "CATEGORY, Id 3": 12.333333333333334, "TASK, Id 1": 30.666666666666668, "TASK, Id 2": 12.333333333333334, "TASK, Id 3": null, "TASK, Id 4": 5, "TASK, Id 5": null, "TASK, Id 6": null, "TASK, Id 7": null, "TASK, Id 8": null, "TASK, Id 9": null, "TASK, Id 10": null, "TASK, Id 12": null, "TASK, Id 14": null, "TASK, Id 16": null, "TASK, Id 17": null, "TASK, Id 26": 12.333333333333334 }, { "name": "Month 8 . Week 2", "CATEGORY, Id 0": 38, "CATEGORY, Id 1": null, "CATEGORY, Id 2": 12, "CATEGORY, Id 3": null, "TASK, Id 1": null, "TASK, Id 2": 15, "TASK, Id 3": null, "TASK, Id 4": null, "TASK, Id 5": null, "TASK, Id 6": 5, "TASK, Id 7": 5, "TASK, Id 8": 5, "TASK, Id 9": 5, "TASK, Id 10": null, "TASK, Id 12": null, "TASK, Id 14": null, "TASK, Id 16": null, "TASK, Id 17": null, "TASK, Id 26": 15 }, { "name": "Month 8 . Week 3", "CATEGORY, Id 0": 7, "CATEGORY, Id 1": 12.333333333333334, "CATEGORY, Id 2": null, "CATEGORY, Id 3": null, "TASK, Id 1": null, "TASK, Id 2": null, "TASK, Id 3": 12.333333333333334, "TASK, Id 4": null, "TASK, Id 5": null, "TASK, Id 6": null, "TASK, Id 7": null, "TASK, Id 8": null, "TASK, Id 9": null, "TASK, Id 10": null, "TASK, Id 12": null, "TASK, Id 14": 7, "TASK, Id 16": null, "TASK, Id 17": null, "TASK, Id 26": null }, { "name": "Month 8 . Week 4", "CATEGORY, Id 0": null, "CATEGORY, Id 1": null, "CATEGORY, Id 2": 10, "CATEGORY, Id 3": 5, "TASK, Id 1": null, "TASK, Id 2": null, "TASK, Id 3": null, "TASK, Id 4": null, "TASK, Id 5": 5, "TASK, Id 6": null, "TASK, Id 7": null, "TASK, Id 8": null, "TASK, Id 9": null, "TASK, Id 10": 5, "TASK, Id 12": 5, "TASK, Id 14": null, "TASK, Id 16": null, "TASK, Id 17": null, "TASK, Id 26": null }, { "name": "Month 8 . Week 5", "CATEGORY, Id 0": 5, "CATEGORY, Id 1": null, "CATEGORY, Id 2": 7, "CATEGORY, Id 3": null, "TASK, Id 1": null, "TASK, Id 2": null, "TASK, Id 3": null, "TASK, Id 4": null, "TASK, Id 5": null, "TASK, Id 6": null, "TASK, Id 7": null, "TASK, Id 8": null, "TASK, Id 9": null, "TASK, Id 10": null, "TASK, Id 12": null, "TASK, Id 14": null, "TASK, Id 16": 7, "TASK, Id 17": 5, "TASK, Id 26": null }, { "name": "Month 9 . Week 1", "CATEGORY, Id 0": 13.333333333333334, "CATEGORY, Id 1": 13.333333333333334, "CATEGORY, Id 3": null, "TASK, Id 11": null, "TASK, Id 14": 6.333333333333333, "TASK, Id 17": null, "TASK, Id 18": 7, "TASK, Id 19": null, "TASK, Id 20": null, "TASK, Id 26": 13.333333333333334 }, { "name": "Month 9 . Week 2", "CATEGORY, Id 0": null, "CATEGORY, Id 1": null, "CATEGORY, Id 3": 13.333333333333334, "TASK, Id 11": 5, "TASK, Id 14": null, "TASK, Id 17": 8.333333333333334, "TASK, Id 18": null, "TASK, Id 19": null, "TASK, Id 20": null, "TASK, Id 26": null }, { "name": "Month 9 . Week 3", "CATEGORY, Id 0": null, "CATEGORY, Id 1": 14, "CATEGORY, Id 3": null, "TASK, Id 11": null, "TASK, Id 14": null, "TASK, Id 17": null, "TASK, Id 18": null, "TASK, Id 19": 7, "TASK, Id 20": 7, "TASK, Id 26": null } ] console.log(Math.max(...[].concat([], ...temp1.map(i => Object.values(i))).filter(v => typeof v === 'number')))

有一件事要注意,数学。Max(1,2,3)返回3。 Math.max(…[1,2,3]),因为当一个对象或数组中的所有元素都需要包含在某种类型的列表中时,可以使用Spread语法。

我们将利用这一点!

让我们假设一个数组看起来像:

var a = [{a: 1, b: 2}, {foo: 12, bar: 141}]

目标是在所有属性中找到最大值(这里是bar(141))

所以要使用Math.max(),我们需要一个数组中的值(所以我们可以…arr)

首先我们把这些数分开 我们可以推断数组a中的每一项都是一个对象。 在遍历每个对象时,Object.values(item)将以数组形式提供该项的所有值,并且我们可以使用map生成一个只有值的新数组

So,

var p = a.map(item => Object.values(item)) // [ [1, 2], [12, 141] ]

此外,使用concat、

[]。concat (arr[],…),或者只是[].concat arr arr(…),[[1,2],[141]]趋于平缓,[141]1、2、12日

So,

var f = [].concat(...p) // [1, 2, 12, 141]

因为我们现在只有一个数字数组,我们执行Math.max(…f):

var m = Math.max(...f) // 141