Home > Back-end >  how to get max date from arrays of objects
how to get max date from arrays of objects

Time:09-02

I want to get the max date of the dates from an array of objects , if dates are null it will just return null right now it returns --MAXDATE Thu Jan 01 1970 08:00:00 GMT 0800 (.... Standard Time). if there no max date from the arrays of object then return max as null .

Any idea guys ? Thanks.

#code

  const maxDate = new Date(
            Math.max(
              ...data.map(element => {
                return new Date(element.MeasureDate);
              }),
            ),
          );
             console.log('--MAXDATE' , maxDate)

#sample data

=

[
  {
    "Address": 25,
    "AlertType": 1,
    "Area": "North",
    "MeasureDate": "2019-02-01T00:01:01.001Z",
    "MeasureValue": -1
  },
  {
    "Address": 26,
    "AlertType": 1,
    "Area": "West",
    "MeasureDate": "2016-04-12T15:13:11.733Z",
    "MeasureValue": -1
  },
  {
    "Address": 25,
    "AlertType": 1,
    "Area": "North",
    "MeasureDate": "2017-02-01T00:01:01.001Z",
    "MeasureValue": -1
  }
          .
          .
          .
]

#sample data2 - output is 2019-02-01T00:01:01.001Z

=

[
  {
    "Address": 25,
    "AlertType": 1,
    "Area": "North",
    "MeasureDate": "2019-02-01T00:01:01.001Z",
    "MeasureValue": -1
  },
  {
    "Address": 26,
    "AlertType": 1,
    "Area": "West",
    "MeasureDate": null,
    "MeasureValue": -1
  },
  {
    "Address": 25,
    "AlertType": 1,
    "Area": "North",
    "MeasureDate": null,
    "MeasureValue": -1
  }
          .
          .
          .
]

#sample data3 - output is null

=

[
  {
    "Address": 25,
    "AlertType": 1,
    "Area": "North",
    "MeasureDate": null,
    "MeasureValue": -1
  },
  {
    "Address": 26,
    "AlertType": 1,
    "Area": "West",
    "MeasureDate": null,
    "MeasureValue": -1
  },
  {
    "Address": 25,
    "AlertType": 1,
    "Area": "North",
    "MeasureDate": null,
    "MeasureValue": -1
  }
          .
          .
          .
]

CodePudding user response:

Here is a solution using reduce:

const data = [
  {
    "Address": 25,
    "AlertType": 1,
    "Area": "North",
    "MeasureDate": "2019-02-01T00:01:01.001Z",
    "MeasureValue": -1
  },
  {
    "Address": 26,
    "AlertType": 1,
    "Area": "West",
    "MeasureDate": "2016-04-12T15:13:11.733Z",
    "MeasureValue": -1
  },
  {
    "Address": 25,
    "AlertType": 1,
    "Area": "North",
    "MeasureDate": "2017-02-01T00:01:01.001Z",
    "MeasureValue": -1
  }
]

const maxDate = data.reduce((max, item) => {
  if (item.MeasureDate) {
    const date = new Date(item.MeasureDate);
    
    if (max)
      max = date > max ? date : max;
    else 
      max = date;
  }
  
  return max;
}, null);

console.log(maxDate)

CodePudding user response:

Here's one approach:

function maxMeasureDate(arr: Array<{ [k: string]: any, MeasureDate: string | null }>) {
    const maxTimestamp = Math.max(...arr.
        map(x => x.MeasureDate !== null ? Date.parse(x.MeasureDate) : -Infinity)
    );
    return (maxTimestamp !== -Infinity) ? new Date(maxTimestamp) : null;
}

We use the Date.parse() method to get the timestamp as a number, and then use the Math.max() function on the results to get the maximum one. Then we pass this maximum to the Date() constructor. We have to account for values where the MeasureDate property is null, and we do this by returning the -Infinity value which is the smallest possible number. This is what comes out of Math.max() if you pass it no input, also. So the only way -Infinity will come out of Math.max() is if there are no non-null values of the MeasureDate property. Thus at the very end we check for -Infinity and if it is that value we return null.

Let's test it out:

const sample1 = [
    {
        "Address": 25,
        "AlertType": 1,
        "Area": "North",
        "MeasureDate": "2019-02-01T00:01:01.001Z",
        "MeasureValue": -1
    },
    {
        "Address": 26,
        "AlertType": 1,
        "Area": "West",
        "MeasureDate": "2016-04-12T15:13:11.733Z",
        "MeasureValue": -1
    },
    {
        "Address": 25,
        "AlertType": 1,
        "Area": "North",
        "MeasureDate": "2017-02-01T00:01:01.001Z",
        "MeasureValue": -1
    }
]
console.log(maxMeasureDate(sample1)); // Date: "2019-02-01T00:01:01.001Z" 

const sample2 = [
    {
        "Address": 25,
        "AlertType": 1,
        "Area": "North",
        "MeasureDate": "2019-02-01T00:01:01.001Z",
        "MeasureValue": -1
    },
    {
        "Address": 26,
        "AlertType": 1,
        "Area": "West",
        "MeasureDate": null,
        "MeasureValue": -1
    },
    {
        "Address": 25,
        "AlertType": 1,
        "Area": "North",
        "MeasureDate": null,
        "MeasureValue": -1
    }
]
console.log(maxMeasureDate(sample2)); // Date: "2019-02-01T00:01:01.001Z" 

const sample3 = [
    {
        "Address": 25,
        "AlertType": 1,
        "Area": "North",
        "MeasureDate": null,
        "MeasureValue": -1
    },
    {
        "Address": 26,
        "AlertType": 1,
        "Area": "West",
        "MeasureDate": null,
        "MeasureValue": -1
    },
    {
        "Address": 25,
        "AlertType": 1,
        "Area": "North",
        "MeasureDate": null,
        "MeasureValue": -1
    }
];
console.log(maxMeasureDate(sample3)); // null

Looks good.

Playground link to code

  • Related