简体   繁体   English

使用Underscore从集合中获取最大和最小键

[英]Get max and min keys from a collection with Underscore

How can I get max and min keys from this kind of a collection with Underscore? 如何使用Underscore从这种集合中获取最大和最小键? Seems to be an easy task but I didn't find a quick solution. 似乎是一项简单的任务,但我找不到快速的解决方案。

{
    "2013-06-26":839,
    "2013-06-25":50,
    "2013-06-22":25,
    "2013-05-14":546,
    "2013-03-11":20
}

Unfortunately, _.min and _.max only support numbers, so we can't use those for your string keys. 不幸的是,_. min和_.max只支持数字,所以我们不能将它们用于你的字符串键。 Fortunately, your dates are in a string-sortable format. 幸运的是,您的日期采用字符串排序格式。

var minkey, maxkey;
_.each(obj, function(value, key) {
   if (minkey == null || key < minkey) { minkey = key; }
});
_.each(obj, function(value, key) {
   if (maxkey == null || key > maxkey) { maxkey = key; }
});

Now, if you really wanted the key of the max/min value , then it's this. 现在,如果你真的想要最大/最小值关键 ,那就是这个。 Luckily, your values are numbers, so that makes it a little easier: 幸运的是,您的值是数字,因此更容易:

var keys = _.keys(obj);
function itemgetter(key) { return obj[key]; }
minkey = _.min(keys, itemgetter);
maxkey = _.max(keys, itemgetter);

Although it's little old question but there is a single line solution for the same, you can chain it. 虽然这个问题不是很老,但是有一个单行解决方案,你可以链接它。

var a ={
    "2013-06-26":839,
    "2013-06-25":50,
    "2013-03-08":25,
    "2013-05-14":546,
    "2013-03-11":20
};
_.chain(a).keys().sort().last().value(); //max: 2013-06-26
_.chain(a).keys().sort().first().value(); //min: 2013-03-08

jsFiddle 的jsfiddle

To get the max and min key values of the object, if this is truly what you mean and not the key of max and min values (perhaps worth clarifying in your question), then for the example you have given, you could do it alternatively in POJS like this. 要获得对象的最大和最小键值,如果这是你的意思而不是最大值和最小值的关键(也许值得在你的问题中澄清),那么对于你给出的例子,你可以选择像这样的POJS。

Javascript 使用Javascript

function getMaxKey(object) {
    var max,
        i;

    for (i in object) {
        if (object.hasOwnProperty(i)) {
            if (!max) {
                max = i;
            } else if (i > max) {
                max = i;
            }
        }
    }

    return max;
}

function getMinKey(object) {
    var min,
        i;

    for (i in object) {
        if (object.hasOwnProperty(i)) {
            if (!min) {
                min = i;
            } else if (i < min) {
                min = i;
            }
        }
    }

    return min;
}

var test = {
    "2013-06-26": 839,
    "2013-06-25": 50,
    "2013-06-22": 25,
    "2013-05-14": 546,
    "2013-03-11": 20
};

console.log(getMaxKey(test));
console.log(getMinKey(test));

Output 产量

2013-06-26
2013-03-11 

On jsfiddle jsfiddle

Or using ECMA5 Object.keys as suggested by @dandavis 或者使用@dandavis建议的ECMA5 Object.keys

Javascript 使用Javascript

function getMaxKey(object) {
    return Object.keys(object).sort()[0];
}

function getMinKey(object) {
    return Object.keys(object).sort().slice(-1)[0]
}

var test = {
    "2013-06-26": 839,
    "2013-06-25": 50,
    "2013-06-22": 25,
    "2013-05-14": 546,
    "2013-03-11": 20
};

console.log(getMaxKey(test));
console.log(getMinKey(test));

On jsfiddle jsfiddle

If on the other hand you want to get the key of the max an min values then you can do this in POJS. 另一方面,如果您想获得最大最小值的关键值,那么您可以在POJS中执行此操作。

Javascript 使用Javascript

function getMaxKey(object) {
    var max,
        key,
        i;

    for (i in object) {
        if (object.hasOwnProperty(i)) {
            if (typeof max !== "number") {
                max = object[i];
                key = i;
            } else if (i > max) {
                max = object[i];
                key = i;
            }
        }
    }

    return key;
}

function getMinKey(object) {
    var min,
        key,
        i;

    for (i in object) {
        if (object.hasOwnProperty(i)) {
            if (typeof min !== "number") {
                min = object[i];
                key = i;
            } else if (object[i] < min) {
                min = object[i];
                key = i;
            }
        }
    }

    return key;
}

var test = {
    "2013-06-26": 839,
    "2013-06-25": 50,
    "2013-06-22": 25,
    "2013-05-14": 546,
    "2013-03-11": 20
};

console.log(getMaxKey(test));
console.log(getMinKey(test));

On jsfiddle jsfiddle

In this case (with your specific test data) the output will be the same as the previous example. 在这种情况下(使用您的特定测试数据),输出将与前一个示例相同。

Or using ECMA5 Object.keys and Array.prototype.reduce 或者使用ECMA5 Object.keysArray.prototype.reduce

Javascript 使用Javascript

function getMaxKey(object) {
    return Object.keys(object).reduce(function (previous, key) {
        return object[key] > object[previous] ? key : previous;
    });
}

function getMinKey(object) {
    return Object.keys(object).reduce(function (previous, key) {
        return object[key] < object[previous] ? key : previous;
    });
}

var test = {
    "2013-06-26": 839,
    "2013-06-25": 50,
    "2013-06-22": 25,
    "2013-05-14": 546,
    "2013-03-11": 20
};

console.log(getMaxKey(test));
console.log(getMinKey(test));

On jsfiddle jsfiddle

This second example is easily accomplished by using the Underscore with some or all of these methods _.keys , _.min and _.max and has been answered already. 第二个例子很容易通过使用_.keys与一些或所有这些方法_.keys_.min_.max并且已经得到了回答。

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

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