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


当前回答

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

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

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

其他回答

或者一个简单的排序!保持真实:)

array.sort((a,b)=>a.y<b.y)[0].y
const getMaxFromListByField = (list, field) => { 
    return list[list.map(it => it[field]).indexOf(Math.max(...list.map(it => it[field])))] 
}

又快又脏:

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

它返回对象简化的@andy polhill answare

var data = ( { y: 90 }, { y: 9 }, { y: 8 } ] Const Max = data。Reduce ((prev, current)=> ((prev, current))Y >电流。Y) ?当前),0)//返回对象 console.log (max)

let List= [{votes:4},{votes:8},{votes:7}]

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

console.log(objMax)