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

其他回答

又快又脏:

Object.defineProperty (Array.prototype“分钟”, { 价值:函数(f) { F = F || (v => v); 返回。Reduce ((a, b) => (f(a) < f(b)) ?A: b); } }); Object.defineProperty (Array.prototype‘麦克斯’, { 价值:函数(f) { F = F || (v => v); 返回。Reduce ((a, b) => (f(a) > f(b)) ?A: b); } }); console.log ([1, 2, 3] .max ()); console.log([1, 2, 3]。Max (x => x*(4-x))); console.log ([1, 2, 3] .min ()); console.log([1, 2, 3]。Min (x => x*(4-x)));

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

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

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

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

每个数组,并得到最大值与数学。

data.reduce((max, b) => Math.max(max, b.costo), data[0].costo);
// 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