我正在寻找一个非常快速,干净和有效的方法来获得以下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。


当前回答

我想一步一步地解释这个简短的公认答案:

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>');

进一步的信息:

Math.max.apply () Array.prototype.map () Array.from () Function.prototype.apply () Array.prototype.filter () Array.prototype.find ()

其他回答

var max = 0;                
jQuery.map(arr, function (obj) {
  if (obj.attr > max)
    max = obj.attr;
});
const getMaxFromListByField = (list, field) => { 
    return list[list.map(it => it[field]).indexOf(Math.max(...list.map(it => it[field])))] 
}
let List= [{votes:4},{votes:8},{votes:7}]

let objMax = List.reduce((max, curren) => max.votes > curren.votes ? max : curren);

console.log(objMax)

在对象数组中找到属性“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)

我想一步一步地解释这个简短的公认答案:

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>');

进一步的信息:

Math.max.apply () Array.prototype.map () Array.from () Function.prototype.apply () Array.prototype.filter () Array.prototype.find ()