Rio*_*Rio 345 javascript json
我正在寻找一种非常快速,干净和有效的方法来获取以下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
}
]
Run Code Online (Sandbox Code Playgroud)
for循环是唯一可行的方法吗?我热衷于某种方式使用Math.max.
tob*_*ies 629
要查找y对象的最大值array:
Math.max.apply(Math, array.map(function(o) { return o.y; }))
Run Code Online (Sandbox Code Playgroud)
And*_*ill 206
一种方法是使用Array reduce ..
const max = data.reduce(function(prev, current) {
return (prev.y > current.y) ? prev : current
}) //returns object
Run Code Online (Sandbox Code Playgroud)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce http://caniuse.com/#search=reduce(IE99及以上版本)
如果您不需要支持IE(仅限Edge),或者可以使用诸如Babel之类的预编译器,则可以使用更简洁的语法.
const max = data.reduce((prev, current) => (prev.y > current.y) ? prev : current)
Run Code Online (Sandbox Code Playgroud)
小智 120
干净简洁的ES6(巴别塔)
const maxValueOfY = Math.max(...arrayToSearchIn.map(o => o.y), 0);
Run Code Online (Sandbox Code Playgroud)
如果arrayToSearchIn为空,则第二个参数应确保默认值.
Guf*_*ffa 24
好吧,首先你应该解析JSON字符串,以便你可以轻松访问它的成员:
var arr = $.parseJSON(str);
Run Code Online (Sandbox Code Playgroud)
使用该map方法提取值:
arr = $.map(arr, function(o){ return o.y; });
Run Code Online (Sandbox Code Playgroud)
然后你可以在max方法中使用数组:
var highest = Math.max.apply(this,arr);
Run Code Online (Sandbox Code Playgroud)
或者作为一个单行:
var highest = Math.max.apply(this,$.map($.parseJSON(str), function(o){ return o.y; }));
Run Code Online (Sandbox Code Playgroud)
con*_*gus 22
我想逐步解释简洁的接受答案:
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>');Run Code Online (Sandbox Code Playgroud)
更多的信息:
Math.max.apply()Array.prototype.map()Array.from()Function.prototype.apply()Array.prototype.filter()Array.prototype.find()Kam*_*ski 19
处理负数大小写的树ONELINERS的比较(在a数组中输入):
var maxA = Math.max(...a.map(o=>o.y),a[0].y); // 33 chars time complexity: >O(2n)
var maxB = a.reduce((a,b)=>a.y>b.y?a:b).y; // 30 chars time complexity: O(n)
var maxC = a.sort((a,b)=>b.y-a.y)[0].y; // 27 chars time complexity: O(nlogn)
Run Code Online (Sandbox Code Playgroud)
可编辑的示例在这里。想法来自:maxA,maxB,maxC(副作用:已更改a- sort已就位)。
var maxA = Math.max(...a.map(o=>o.y),a[0].y); // 33 chars time complexity: >O(2n)
var maxB = a.reduce((a,b)=>a.y>b.y?a:b).y; // 30 chars time complexity: O(n)
var maxC = a.sort((a,b)=>b.y-a.y)[0].y; // 27 chars time complexity: O(nlogn)
Run Code Online (Sandbox Code Playgroud)
对于更大的阵列,Math.max...将抛出异常:超出最大调用堆栈大小(Chrome 76.0.3809,Safari 12.1.2,日期2019-09-13)
var a = [
{"x":"8/11/2009","y":0.026572007},{"x":"8/12/2009","y":0.025057454},
{"x":"8/14/2009","y":0.031004457},{"x":"8/13/2009","y":0.024530916}
]
var maxA = Math.max(...a.map(o=>o.y),a[0].y);
var maxB = a.reduce((a,b)=>a.y>b.y?a:b).y;
var maxC = a.sort((a,b)=>b.y-a.y)[0].y;
document.body.innerHTML=`<pre>maxA: ${maxA}\nmaxB: ${maxB}\nmaxC: ${maxC}</pre>`;Run Code Online (Sandbox Code Playgroud)
小智 13
这是最短的解决方案(One Liner)ES6:
Math.max(...values.map(o => o.y));
Run Code Online (Sandbox Code Playgroud)
小智 10
var data = [
{ 'name': 'Vins', 'age': 27 },
{ 'name': 'Jan', 'age': 38 },
{ 'name': 'Alex', 'age': 80 },
{ 'name': 'Carl', 'age': 25 },
{ 'name': 'Digi', 'age': 40 }
];
var max = data.reduce(function (prev, current) {
return (prev.age > current.age) ? prev : current
});
//output = {'name': 'Alex', 'age': 80}
Run Code Online (Sandbox Code Playgroud)
使用一个函数找到最大 obj.value 返回 obj
let List= [{votes:4},{votes:8},{votes:7}]
let objMax = List.reduce((max, curren) => max.votes > curren.votes ? max : curren);
console.log(objMax)
Run Code Online (Sandbox Code Playgroud)
或者使用两个函数返回最大值
Math.max(...List.map(el => el.votes)))
Run Code Online (Sandbox Code Playgroud)
如果你(或者这里有人)可以自由使用lodash实用程序库,它有一个maxBy函数,在你的情况下非常方便.
因此你可以这样使用:
_.maxBy(jsonSlice, 'y');
Run Code Online (Sandbox Code Playgroud)
小智 5
或者简单的排序!保持真实:)
array.sort((a,b)=>a.y<b.y)[0].y
Run Code Online (Sandbox Code Playgroud)
每个数组并使用 Math 获取最大值。
data.reduce((max, b) => Math.max(max, b.costo), data[0].costo);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
236773 次 |
| 最近记录: |