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

2020/09/27 09:01 · javascript ·  · 0评论

``````[
{
"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 o.y; }))
``````

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

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

``````const max = data.reduce((prev, current) => (prev.y > current.y) ? prev : current)
``````

``````const maxValueOfY = Math.max(...arrayToSearchIn.map(o => o.y), 0);
``````

``````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)
``````

``````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 o.x; });
// es6
xValues = Array.from(objects, o => o.x);

// 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 o.x === xMax; });

// Altogether
xMax = Math.max.apply(null, objects.map(function(o) { return o.x; }));
var maxXObject = objects.filter(function(o) { return o.x === xMax; })[0];
// es6
xMax = Math.max(...Array.from(objects, o => o.x));
maxXObject = objects.find(o => o.x === 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>');``````

``````var arr = \$.parseJSON(str);
``````

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

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

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

``````_.maxBy(jsonSlice, 'y');
``````

``````array.sort((a,b)=>a.y<b.y)[0].y
``````

``````Math.max(...values.map(o => o.y));
``````

``````data.reduce((max, b) => Math.max(max, b.costo), data[0].costo);
``````
``````var max = 0;
jQuery.map(arr, function (obj) {
if (obj.attr > max)
max = obj.attr;
});
``````

ES6解决方案

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

``````Here is very simple way to go:

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
``````