可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm looking for a really quick, clean and efficient way to get the max "y" value in the following JSON slice:
[{"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}]
Is a for-loop the only way to go about it? I'm keen on somehow using Math.max
.
回答1:
Math.max.apply(Math,array.map(function(o){return o.y;}))
回答2:
One way would be to use 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 and above)
If you don't need to support IE (only Edge), or can use a pre-compiler such as Babel you could use the more terse syntax.
const max = data.reduce((prev, current) => (prev.y > current.y) ? prev : current)
回答3:
clean and simple ES6 (Babel)
const maxValueOfY = Math.max(...arrayToSearchIn.map(o => o.y));
回答4:
Well, first you should parse the JSON string, so that you can easily access it's members:
var arr = $.parseJSON(str);
Use the map
method to extract the values:
arr = $.map(arr, function(o){ return o.y; });
Then you can use the array in the max
method:
var highest = Math.max.apply(this,arr);
Or as a one-liner:
var highest = Math.max.apply(this,$.map($.parseJSON(str), function(o){ return o.y; }));
回答5:
I'd like to explain the terse accepted answer step-by-step:
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('objects: ' + JSON.stringify(objects) + '
'); document.write('xValues: ' + JSON.stringify(xValues) + '
'); document.write('xMax: ' + JSON.stringify(xMax) + '
'); document.write('maxXObjects: ' + JSON.stringify(maxXObjects) + '
'); document.write('maxXObject: ' + JSON.stringify(maxXObject) + '
');
Further information:
回答6:
if you (or, someone here) are free to use lodash
utility library, it has a maxBy function which would be very handy in your case.
hence you can use as such:
_.maxBy(jsonSlice, 'y');
回答7:
var max = 0; jQuery.map(arr, function (obj) { if (obj.attr > max) max = obj.attr; });
回答8:
use this both : obj = Math.max.apply(Math,data.map(function(o){return o.partsCount;})); Array.prototype.max = function() { return Math.max.apply(null, this); };
回答9:
Based on great answer from @tobyodavies.
Try this out below working example.
var t = [{"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}] Math.max.apply(Math,t.map(function(o){return o.y;}))
回答10:
function getMaxProperty(arrayOfObjects, property) { const arrayOfValues = arrayOfObjects.map(obj => obj[property]); return Math.max(...arrayOfValues); }
回答11:
A simple reduce with negative infinity as default? Does what you need in a single iteration/operation...
array.reduce((last,current)=>current.y>last?current.y:last,-Infinity)
回答12:
Or a simple sort! Keeping it real :)
array.sort((a,b)=>a.y
回答13: