查找对象数组中属性的最大值

我正在寻找一个非常快速,干净而高效的方法来获得下面的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-loop唯一的方法去呢? 我很想使用Math.max

 Math.max.apply(Math,array.map(function(o){return oy;})) 

一种方法是使用数组减less..

 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) 

那么,首先你应该parsingJSONstring,以便您可以轻松访问它的成员:

 var arr = $.parseJSON(str); 

使用map方法提取值:

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

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

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

或者作为一个单线:

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

干净简单的ES6(巴别)

 const maxValueOfY = Math.max(...arrayToSearchIn.map(o => oy)); 

我想详细解释一下接受的答案 :

 var objects = [{ x: 3 }, { x: 1 }, { x: 2 }]; // array.map lets you extract an array of attribute values var xValues = objects.map(function(o) { return ox; }); // es6 xValues = Array.from(objects, o => ox); // function.apply lets you expand an array argument as individual arguments // So the following is equivalent to Math.max(3, 1, 2) // The first argument is "this" but since Math.max doesn't need it, null is fine var xMax = Math.max.apply(null, xValues); // es6 xMax = Math.max(...xValues); // Finally, to find the object that has the maximum x value (note that result is array): var maxXObjects = objects.filter(function(o) { return ox === xMax; }); // Altogether xMax = Math.max.apply(null, objects.map(function(o) { return ox; })); var maxXObject = objects.filter(function(o) { return ox === xMax; })[0]; // es6 xMax = Math.max(...Array.from(objects, o => ox)); maxXObject = objects.find(o => ox === xMax); document.write('<p>objects: ' + JSON.stringify(objects) + '</p>'); document.write('<p>xValues: ' + JSON.stringify(xValues) + '</p>'); document.write('<p>xMax: ' + JSON.stringify(xMax) + '</p>'); document.write('<p>maxXObjects: ' + JSON.stringify(maxXObjects) + '</p>'); document.write('<p>maxXObject: ' + JSON.stringify(maxXObject) + '</p>'); 

如果你(或者某人在这里)可以自由使用lodash实用程序库,那么它有一个maxBy函数,在你的情况下会非常方便。

因此你可以这样使用:

 _.maxBy(jsonSlice, 'y'); 
 var max = 0; jQuery.map(arr, function (obj) { if (obj.attr > max) max = obj.attr; }); 
 use this both : obj = Math.max.apply(Math,data.map(function(o){return o.partsCount;})); Array.prototype.max = function() { return Math.max.apply(null, this); }; 

根据@tobyodavies的很好的回答。

尝试下面的工作示例。

 var t = [{"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}] Math.max.apply(Math,t.map(function(o){return oy;})) 
 function getMaxProperty(arrayOfObjects, property) { const arrayOfValues = arrayOfObjects.map(obj => obj[property]); return Math.max(...arrayOfValues); } 

ES6解决scheme

Math.max(...array.map(function(o){return oy;}))

有关更多详细信息,请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max