My data consists of an array filled with objects like this
data = [
{
mas_name: (...),
mas_plan_end: (...) // 'YYYY-MM-DD' eg: '2021-03-19'
mas_plan_start: (...) // 'YYYY-MM-DD' eg: '2021-03-19'
...
},
{
mas_name: (...),
mas_plan_end: (...)
mas_plan_start: (...)
...
}
]
Within the array, some objects may have mas_plan_start set to null. I aim to extract both the minimum and maximum values of mas_plan_start from this dataset. Attempting this task, I utilized moments in the following manner:
const max = moment.max(children.map((o) =>
o.mas_plan_start ? o.mas_plan_start : moment(new Date()).format("YYYY-MM-DD")
));
const min = moment.min(children.map((o) =>
o.mas_plan_start ? o.mas_plan_start : moment(new Date()).format("YYYY-MM-DD")
));
This approach led to the error depicted https://i.sstatic.net/3oiYh.png
Is there a way to resolve this issue or alternatively achieve the same goal without relying on moments?
To address this challenge, consider employing the following solution:
const max_date = moment.max(data.filter((o) =>
o.mas_plan_end !== null
).map((date) =>
moment(date.mas_plan_end, 'YYYY-MM-DD') // conversion to moment object for .max command functionality
));
parent.mas_plan_end = max_date._i; // retrieving date String from moment instance