Get max and min keys from a collection using Underscore

How can I get the maximum and minimum keys from this type of collection using Underscore? This seems to be an easy task, but I did not find a quick fix.

{ "2013-06-26":839, "2013-06-25":50, "2013-06-22":25, "2013-05-14":546, "2013-03-11":20 } 
+4
source share
3 answers

Although this is a small old question, but there is a one-line solution for it, you can link 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

+2
source

Unfortunately, _.min and _.max only support numbers, so we cannot use them for your string keys. Fortunately, your dates are in row-sorted 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 need a max / min value key , then this. Fortunately, your values ​​are numbers, so this will simplify things a bit:

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

To get the maximum and minimum values ​​of the object key, if this is really what you have in mind, and not the key of the max and min values ​​(maybe it’s worth clarifying in your question), then for the example that you indicated, you can do it alternatively in pojs like that.

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 

Jsfiddle on

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)); 

Jsfiddle on

If, on the other hand, you want to get the key from the maximum values ​​of min, then you can do it 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)); 

Jsfiddle on

In this case (with your specific test data) the output will be the same as in 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)); 

Jsfiddle on

This second example is easily executed using Underscore with some or all of these _.keys , _.min and _.max methods and the _.max has already been given.

0
source

Source: https://habr.com/ru/post/1488338/


All Articles