0

I have an object:

{
    "status": 200,
    "error": false,
    "msg": "Successful response",
    "data": {
        "horodate": [ "2016-10-13 00:00:00", "2016-10-13 00:30:00", ... ], 
        "value": [ 2609.479, 2390.026, 2320.394, 2276.602, 2247.151, ... ]
    }
}

I'm getting the max and min value:

$.getJSON(url, function (data) {
    // ...
    var arr = Object.keys(data['data']['value']).map(function(key) { 
        return data['data']['value'][key]; 
    });
    var min = Math.min.apply(null, arr);
    var max = Math.max.apply(null, arr);
    console.log(min); //2609.479
    console.log(max); //2247.151

But how can I get the horodate of those min and max values when I retrieve min and max from the object?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
ramsey_lewis
  • 558
  • 8
  • 25

3 Answers3

3

The problem is you disconnect the min/max from the data. So either you need a different way to get the min max or you need to loop over the set and match the min/max. I would do it a different way.

I would loop over the set and do the check right than and there to look for the min and max. This way you are doing one loop.

var result = {
  "status": 200,
  "error": false,
  "msg": "Successful response",
  "data": {
    "horodate": ["2016-10-13 00:00:00", "2016-10-13 00:30:00", "x", "y", "z"],
    "value": [2609.479, 2390.026, 2320.394, 2276.602, 2247.151]
  }
}

function getMinMax(data) {
   var details = {  //store first index for min and max
       minV : data.value[0],
       maxV : data.value[0],
       minH : data.horodate[0],
       maxH : data.horodate[0]
     };
   return data.value.reduce( function (d, val, ind ){  //I used reduce, you can use a for loop or forEach
     if (d.minV > val) {  //see if we have a min
       d.minV = val;
       d.minH = data.horodate[ind];
     }
     else if (d.maxV < val) {  //see if we have a max
       d.maxV = val;
       d.maxH = data.horodate[ind];
     }
     
     return d;
   }, details );
  
  
}

var results = getMinMax(result.data);
console.log(results);

Since OP thinks it is complex, going to make it just a for loop instead of reduce.

var result = {
  "status": 200,
  "error": false,
  "msg": "Successful response",
  "data": {
    "horodate": ["2016-10-13 00:00:00", "2016-10-13 00:30:00", "x", "y", "z"],
    "value": [2609.479, 2390.026, 2320.394, 2276.602, 2247.151]
  }
}

function getMinMax(data) {
  var details = { //store first index for min and max
    minV: data.value[0],
    maxV: data.value[0],
    minH: data.horodate[0],
    maxH: data.horodate[0]
  };

  //loop over the values
  for (var ind = 0; ind < data.value.length; ind++) {
    var val = data.value[ind];
    if (details.minV > val) { //see if we have a min
      details.minV = val;
      details.minH = data.horodate[ind];
    } else if (details.maxV < val) { //see if we have a max
      details.maxV = val;
      details.maxH = data.horodate[ind];
    }
  }
  //return the results
  return details;
}

var results = getMinMax(result.data);
console.log(results);
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Thansk you ! it work as well but mohamed-ibrahim's answer is easier to understand for me. i gave you +1 – ramsey_lewis Oct 13 '16 at 14:33
  • It is just a loop and not multiple look ups. The other answer requires a lot more processing. All reduce is doing is a loop, it can easily just be a for loop. – epascarello Oct 13 '16 at 14:34
  • I added a for loop so you can see what reduce is doing. In the end either answer should work. My answer does everything in one loop, the other answer requires multiple loops (one loop to get min, one loop to get max, one to find the value with indexOf, and another to find the value with indexOf) – epascarello Oct 13 '16 at 14:47
  • 1
    @Ra3IDeN min, max, indexOf are loops...internally, the min/max/indexof solution might be faster, too busy to do a perf test. – epascarello Oct 13 '16 at 14:51
1

You can get the min index and then get the horodate corresponding to this index:

data = {
  "status": 200,
  "error": false,
  "msg": "Successful response",
  "data": {
    "horodate": [ "2016-10-13 00:00:00", "2016-10-13 00:30:00", "2016-10-13 01:00:00", "2016-10-13 01:30:00", "2016-10-13 02:00:00"], 
    "value": [ 2609.479, 2390.026, 2320.394, 2276.602, 2247.151]
  }
}
var values_arr = data['data']['value']  
var horodates_arr = data['data']['horodate']  

var date_for_value = function(value){
  var min_index = values_arr.indexOf(value);
  return horodates_arr[min_index];  
}

var min = {'value': Math.min.apply(null, values_arr),
           'horodate': date_for_value(min['value'])}

var max = {'value': Math.max.apply(null, values_arr),
           'horodate': date_for_value(max['value'])}

console.log(min); //Object { value=2247.151,  horodate="2016-10-13 02:00:00"}
console.log(max); //Object { value=2609.479,  horodate="2016-10-13 00:00:00"}
mohamed-ibrahim
  • 10,837
  • 4
  • 39
  • 51
0
$(function() {
    var result = {
        status: 200,
        error: false,
        msg: "Successful response",
        data: {
            "horodate": ["2016-10-13 00:00:00", "2016-10-13 00:30:00", "x", "y", "z"],
            "value": [2609.479, 2390.026, 2320.394, 2276.602, 2247.151]
        }
    }

    var highest = 0; // for the highest value
    var lowest = Number.MAX_VALUE; // for the lowest value
    var lowIndex = 0; // for the lowest date
    var highndex = 0; // for the highest date
    var obj = {}; // object that hold all the info

    $(result.data.value)
        .each(function(i, v) {

            if (v > highest) {
                highest = v;
                highndex = i
            }

            if (v < lowest) {
                lowest = v;
                lowIndex = i
            }
           // if it is the last item in the array then set values
            if ((i + 1) == result.data.value.length) { 
                obj.highest = highest;
                obj.lowest = lowest;
                obj.dateHigh = result.data.horodate[highndex]
                obj.dateLow = result.data.horodate[lowIndex]
            }


        })
    console.log(obj);
});

fiddle

ThunD3eR
  • 3,216
  • 5
  • 50
  • 94