1

I'm having trouble figuring out how to get min/max value out of this object:

 "Prices" : {                                                                                                                                                                                                                                  
                "2014,1,11,0,0,0" : 1089,                                                                                                                                                                                                            
                "2014,1,12,0,0,0" : 1081,                                                                                                                                                                                                            
                "2014,1,13,0,0,0" : 1077,                                                                                                                                                                                                            
                "2014,1,14,0,0,0" : 1069,                                                                                                                                                                                                            
                "2014,1,17,0,0,0" : 1078,                                                                                                                                                                                                            
                "2014,1,18,0,0,0" : 1089,                                                                                                                                                                                                            
                "2014,1,19,0,0,0" : 1095
            }

I want to get the min/max of the numbers (Not dates) I was looking into _.pluck but it requires the key name but I don't know it. It's dates.... Any way to by pass that?

3 Answers 3

2

You can either use a for..in loop or Object.keys() in vanilla Javascript, I'm sure Underscore has wrappers of some sort for those, too.

var p = {
  '2014,1,11,0,0,0': 1089,
  '2014,1,12,0,0,0': 1081,
  '2014,1,13,0,0,0': 1077,
  '2014,1,14,0,0,0': 1069,
  '2014,1,17,0,0,0': 1078,
  '2014,1,18,0,0,0': 1089,
  '2014,1,19,0,0,0': 1095
};
var min = Number.POSITIVE_INFINITY;
var max = Number.NEGATIVE_INFINITY;
for (var prop in p) {
  if (p.hasOwnProperty(prop)) {
    min = Math.min(min, p[prop]);
    max = Math.max(max, p[prop]);
  }
}
console.log('min: ' + min);
console.log('max: ' + max);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use _.values(prices). With this approach you can then use _.keys(prices) and use that as a lookup for the correct key.

http://jsfiddle.net/x7eewf1d/2/

Although it may not be as efficient as other solutions, it will make clear to the next developer (who may be you) what is happening.

var prices = {
  '2014,1,11,0,0,0': 1089,
  '2014,1,12,0,0,0': 1081,
  '2014,1,13,0,0,0': 1077,
  '2014,1,14,0,0,0': 1069,
  '2014,1,17,0,0,0': 1078,
  '2014,1,18,0,0,0': 1089,
  '2014,1,19,0,0,0': 1095
};

var vals = _.values(prices);
var dates = _.keys(prices);

var min = _.min(vals);
var max = _.max(vals);

var minDate = dates[vals.indexOf(min)];
var maxDate = dates[vals.indexOf(max)];;

Comments

0

Another plain JS option is using Object.keys to get an array of the keys, map to get an array of the values, and finally Math.max to get the maximum value:

var p = {
  '2014,1,11,0,0,0': 1089,
  '2014,1,12,0,0,0': 1081,
  '2014,1,13,0,0,0': 1077,
  '2014,1,14,0,0,0': 1069,
  '2014,1,17,0,0,0': 1078,
  '2014,1,18,0,0,0': 1089,
  '2014,1,19,0,0,0': 1095
};

Math.max.apply(Math, Object.keys(p).map(function(key) {return p[key]})); // 1095

and to get the minimum:

Math.min.apply(Math, Object.keys(p).map(function(key) {return p[key]})); // 1069

A for..in loop may be more code than the above, but it's probably a lot faster.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.