I have an array of objects with a date value. I want to filter the array based on the selectedDate and get the Max date in the list of dates. In the below code, I am filtering the array based on the month. Here I get 3 values after filtering, now I want to compare those values and get the MAX Date() value.
How can I do that in Angular or ES6 way?
let selectedDate = new Date();
let array = [{
"date": "2022-08-30T23:00:00Z",
"value": "4.0"
},
{
"date": "2022-08-28T23:00:00Z",
"value": "8.0"
},
{
"date": "2022-08-25T23:00:00Z",
"value": "2.0"
},
{
"date": "2022-07-25T23:00:00Z",
"value": "2.0"
}
];
let x = array.filter(d =>
new Date(d.date).getMonth() === selectedDate.getMonth() - 1
)
console.log(x)
Expected Output:
{
"date": "2022-08-30T23:00:00Z",
"value": "4.0"
}