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


当前回答

它返回对象简化的@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)

其他回答

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

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

求数组中所有对象的y的最大值:

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

或者在更现代的JavaScript中:

    Math.max(...array.map(o => o.y))

首先,你应该解析JSON字符串,这样你就可以很容易地访问它的成员:

var arr = $.parseJSON(str);

使用map方法提取值:

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

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

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

或者作为一行语句:

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

又快又脏:

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

比较三个处理负号大小写的oneliner(在数组中输入):

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)

这里是可编辑的示例。来自:maxA, maxB和maxC的想法(maxB的副作用是数组a被改变,因为排序是到位的)。

var a = [ {" x ": 8/11/2009”、“y”:026572007 {" x "}表示:“8/12/2009”、“y”:025057454} {" x ": 8/14/2009”、“y”:031004457 {" x "}表示:“8/13/2009”、“y”:024530916} ] var maxA = a.reduce((a,b)=>a.y>b.y?a:b) y; var maxC = Math.max(.. a.map(o=>o.y)); var maxB = a.sort((a,b)=>b.y-a.y)[0].y; 文件。身体。innerHTML = ' < pre - > maxA: $ {maxA} \ nmaxB: $ {maxB} nmaxC: $ {maxC} < / pre - > ';

对于较大的数组,Math.max…最大调用堆栈大小超过(Chrome 76.0.3809, Safari 12.1.2,日期2019-09-13)

let a =数组(400*400)。Fill ({"x": "8/11/2009", "y": 0.026572007}); // Exception:超过最大调用堆栈大小 尝试{ let max1= Math.max。apply(Math, a.p ap(o => o.y)); } catch(e) {console.error('Math.max. error ')应用:',e.message)} 尝试{ let max2= Math.max(…a.map(o=>o.y)); } catch(e) {console.error('Math. error ')Max-map:', e.message)}

4元素数组的基准测试