-1

I've got some json that returns a number of dates like this:

{
  "items": [{
    "metafield1": "May 22 2019"
  }, {
    "metafield1": null
  }, {
    "metafield1": null
  }, {
    "metafield1": "May 20 2019"
  }]
}

I've then got an ajax call which gets the data and loops through it to check if it doesn't equal null

success: function(result) {
        var obj = JSON.parse(result);
                obj = obj["items"];
                for(var i of obj){
    if(i.metafield1 !== null){
        console.log(i.metafield1);
    }
}

This returns something like this

May 22 2019

May 20 2019

I now need to compare these dates and find the highest (the one the furthest into the future). I know I can do the date comparison with the Date object like

var date1 = May 22 2019
var date2 = May 20 2019

var newdate1 = new Date(date1)
var newdate2 = new Date(date2)
if (newdate1 > newdate2){
   var biggest_date = newdate1
}
else {
   var biggest_date = newdate2
}

Or something like this. My issue is how do I assign those within my loop and make it work if it's more than two items in the loop. So let's say my loop returns this

May 22 2019

May 20 2019

July 20 2019

How can I get and compare those three values (which are dynamic, so it varies how many of them there are) to find the biggest date?

Community
  • 1
  • 1
MariaL
  • 820
  • 1
  • 12
  • 29

3 Answers3

0

You can try following

  • Filter the items array with not null metafield1
  • Reduce the resulting array to return the object with biggest date

let obj = {"items": [{"metafield1": "May 22 2019"}, {"metafield1": null}, {"metafield1": null}, {"metafield1": "May 20 2019"}, {"metafield1": "June 20 2019"}]};

let biggest_date = obj.items.filter(v => v.metafield1)
                  .reduce((a,c) => new Date(a.metafield1) - new Date(c.metafield1) > 0 ? a : c)
                  .metafield1;
console.log(biggest_date);
Nikhil Aggarwal
  • 26,884
  • 3
  • 37
  • 53
0

You can use reduce to loop thru the array and check if metafield1 is not null, if it is not, compare the dates

let obj = {"items":[{"metafield1":"May 22 2019"},{"metafield1":null},{"metafield1":null},{"metafield1":"May 20 2019"}]};

let result = obj.items.reduce((c, v) => {
  if (v.metafield1) return new Date(c) > new Date(v.metafield1) ? c : v.metafield1;
  return c;
}, null);

console.log(result);

Shorter Version:

Since new Date(null) will result to 1970-01-01T00:00:00.000Z, you can also:

let obj = {"items":[{"metafield1":"May 22 2019"},{"metafield1":null},{"metafield1":null},{"metafield1":"May 20 2019"}]};

let result = obj.items.reduce((c, v) => new Date(c) > new Date(v.metafield1) ? c : v.metafield1, null);

console.log(result);
Eddie
  • 25,279
  • 6
  • 26
  • 53
0
  1. Filter the null value using Array#filter.
  2. And then Convert the date string to time using new Date().getTime() .
  3. Finally use Math.max() get highest value .Then find the index again get the value from using array[index]

var arr = { "items": [{ "metafield1": "May 22 2019" }, { "metafield1": null }, { "metafield1": null }, { "metafield1": "May 20 2019" }] }

var a =arr.items.map(i=> i.metafield1).filter(i=> i)
var m_arr = a.map(d=> new Date(d).getTime());
var res = a[m_arr.indexOf(Math.max(...m_arr))];

console.log(res)
prasanth
  • 19,775
  • 3
  • 25
  • 48