I have an array of this form:
[
{
username: '',
id: '',
moreInfo: {
infoDate: ''
}
}
]
And I need to filter based on the infoDate, whether it's in between two specific dates.
I have a function that accepts the object, and the field to search range by and returns :
return resultDate >= fromDate && resultDate <= thruDate;
But how do I filter such array . I tried
userData.filter(userData => functionthatFiltersDate(userData.moreInfo, {from,thru}, 'infoDate')
The functionthatFiltersDate is a function that accepts an object as input and dates to check range :
functionthatFiltersDate = (
result,
{ fromDate, thruDate },
fieldName
) => {
let resultDate = result[fieldName];
if (!isDate(resultDate)) {
resultDate = moment(result[fieldName]).toDate();
}
if (!isDate(fromDate)) {
fromDate = moment(fromDate).toDate();
}
if (!isDate(thruDate)) {
thruDate = moment(thruDate).toDate();
}
return resultDate >= fromDate && resultDate <= thruDate;
};
How do I filter though for an array of objects, based on another object property that's inside? Any help is appreciated!