1

I need to extract closest lower value and closest higher value in a javascript objet. My logic is to iterate on the objet and put in a variable all values which are inferior than the value of my product (then do the same with the closest higher value and all values which are superior)

The value that I want to extract in that example is the date that is the first value of the object which is constructed like this : "object":[[val1, val2],[val1, val2]]

Do you think my logic is the good one, and can you see what's wrong with my code?

var datagrph = $.parseJSON('{"label":"Yo", "fromDate":"2015-06-05", "launchDate":"2016-08-15", "dateValues":[["2014-06-05",1723.76], ["2015-06-29",1523.76], ["2015-12-29",1023.76], ["2017-01-29",523.76], ["2017-02-22",1523.76], ["2017-03-29",523.76], ["2017-04-29",1523.76], ["2017-05-29",23.76], ["2017-06-21",523.76] ]}');
var productCour = datagrph.dateValues;
var launchDate = datagrph.launchDate;

var val1 = [0];

Object.keys(productCour).map(function(objectDate, index) {
    var value = object[objectDate];
    if (value <= launchDate) {
        val1.push(value);
    } else if (value > launchDate) {
        value++;
    }
    return val1;
});


alert(val1);
Cœur
  • 37,241
  • 25
  • 195
  • 267
Kat
  • 161
  • 1
  • 4
  • 14
  • value is still an array... And i dont get it: what is your desired output?? – Jonas Wilms Jun 30 '17 at 10:27
  • Better way is to calculate the difference from each value and then return the value which has the minimum difference from the current value – abhishekkannojia Jun 30 '17 at 10:27
  • @Jonasw for me value is the specific val1 (the date), am I wrong ? My desired output is val1 the correspond every values that are smaller than the objectDate. – Kat Jun 30 '17 at 11:19
  • @katchou it would be an array if object woupdnt be undefined. So you want to find the value that corresponds to the nearest date to launchDate right? – Jonas Wilms Jun 30 '17 at 11:21
  • @abhishekkannojia you mean something like this ? https://stackoverflow.com/questions/8584902/get-closest-number-out-of-array – Kat Jun 30 '17 at 11:23
  • @Jonasw that's correct – Kat Jun 30 '17 at 11:24

1 Answers1

0

Simply calculate the difference between the current date and launchDate, then set the values accordingly:

var datagrph = $.parseJSON('{"label":"Yo", "fromDate":"2015-06-05", "launchDate":"2016-08-15", "dateValues":[["2014-06-05",1723.76], ["2015-06-29",1523.76], ["2015-12-29",1023.76], ["2017-01-29",523.76], ["2017-02-22",1523.76], ["2017-03-29",523.76], ["2017-04-29",1523.76], ["2017-05-29",23.76], ["2017-06-21",523.76] ]}');
var productCour = datagrph.dateValues;

var launchDate = +new Date(datagrph.launchDate+" T00:00");

var before,after,diffmin,diffmax;

productCour.forEach(function(pair){

 var current=+(new Date(pair[0]+"T00:00"));
 var difference=launchDate-current;

 if(difference>0){
    //launchDate not reached
    if(!diffmin || diffmin>difference){
       //weve got a new before
       before=pair[1];
       diffmin=difference;
    }
  }else{
    //launchDate already reached
    if(!diffmax || diffmax<difference){
       //weve got a new after
       after=pair[1];
       diffmax=difference;
    }
  }
});

before and after should now contain the nearest values.

before:1023.76
after: 523.76

http://jsbin.com/cozovecaha/edit?console

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • Quite impressive, thank you. Seems that we are close, but in after value (pair[0]) the result is "2017-06-21" it should be "2017-01-29" – Kat Jun 30 '17 at 13:11