简体   繁体   中英

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? 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. 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

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.

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

Or using ECMA5 Object.keys as suggested by @dandavis

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

If on the other hand you want to get the key of the max an min values then you can do this in POJS.

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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