简体   繁体   English

从JavaScript数组中获取对象值的最大值和最小值

[英]Get max and min of object values from JavaScript array

What is the best way to get the maximum and minimum values from a JavaScript array of objects? 从JavaScript对象数组中获取最大值和最小值的最佳方法是什么?

Given: 鉴于:

var a = [{x:1,y:0},{x:-1,y:10},{x:12,y:20},{x:61,y:10}];
var minX = Infinity, maxX = -Infinity;
for( var x in a ){
  if( minX > a[x].x )
     minX = a[x].x;
  if( maxX < a[x].x )
     maxX = a[x].x;
}

Seems a bit clumsy. 似乎有点笨拙。 Is there a more elegant way, perhaps using dojo? 是否有更优雅的方式,也许使用道场?

It won't be more efficient, but just for grins: 它不会更有效率,只是为了咧嘴笑:

var minX = Math.min.apply(Math, a.map(function(val) { return val.x; }));
var maxX = Math.max.apply(Math, a.map(function(val) { return val.x; }));

Or if you're willing to have three lines of code: 或者如果你愿意有三行代码:

var xVals = a.map(function(val) { return val.x; });
var minX  = Math.min.apply(Math, xVals);
var maxX  = Math.max.apply(Math, xVals);

Use this example 使用此示例

var lowest = Number.POSITIVE_INFINITY;
var highest = Number.NEGATIVE_INFINITY;
var tmp;
for (var i=myArray.length-1; i>=0; i--) {
    tmp = myArray[i].Cost;
    if (tmp < lowest) lowest = tmp;
    if (tmp > highest) highest = tmp;
}
console.log(highest, lowest);

You could use sort . 你可以使用sort This method modifies the original array, so you might need to clone it : 此方法修改原始数组,因此您可能需要克隆它:

var b = [].concat(a); // clones "a"
b.sort(function (a, b) { return a.x - b.x; });
var min = b[0];
var max = b[b.length - 1];

I know its a little too late, but for newer users you could use lodash . 我知道它有点太晚了,但对于新用户,你可以使用lodash It makes the stuff a lot simpler. 它使事情变得更加简单。

var a = [{x:1,y:0},{x:-1,y:10},{x:12,y:20},{x:61,y:10}];

var X = [];
var Y = [];
a.map(function (val) {
    X.push(val.x);
    Y.push(val.y);
});

var minX = _.min(X);
var minY = _.min(Y);
var maxX = _.max(X);
var maxY = _.max(Y);

Or you could use .sort() to the task as procrastinator explained. 或者你可以使用.sort()来执行procrastinator解释的任务。

Another idea is to calculate max/min by reducing the values to one value. 另一个想法是通过将值减少到一个值来计算最大值/最小值。 This is exactly same as your version in terms of time complexity but a bit different way to think. 就时间复杂度而言,这与您的版本完全相同,但思考的方式有点不同。 ( reduce() is supported since JavaScript 1.8.) (从JavaScript 1.8开始支持reduce() 。)

var getMax = function (field) {
    return a.reduce(function (acc, c) {
        return Math.max(c[field], acc);
    }, -Infinity);
}

var getMin = function (field) {
    return a.reduce(function (acc, c) {
        return Math.min(c[field], acc);
    }, Infinity);
}

console.log(getMax('x')) //61
console.log(getMin('x')) //-1
console.log(getMax('y')) //20
console.log(getMin('y')) //0

You can use map functionality, but it is pretty much just a syntactic sugar around for . 您可以使用map功能,但它是非常简单,只是一个语法糖各地for Any solution using reduce would be twice as slow as your "naive" because it would iterate array once for min value and once more for max. 任何使用reduce解决方案都会比你的“天真”慢两倍,因为它会迭代数组一次为最小值,再次为最大值。 Your current solution is pretty much the best you can have in terms of performance. 您当前的解决方案几乎是性能方面的最佳解决方案。 All you can do is to shave off some more lookups by caching them. 你所能做的就是通过缓存它们来削减更多的查找。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM