I am struggling to filter JSON data based on a specific date using Array.Filter and Map. The code I have tried below is not yielding the desired results. Can someone please provide guidance on how to effectively filter JSON data based on a particular date using filter and map functions?
I am encountering difficulties in comparing the date filter value within the availabilities objects in my code.
Below is the code snippet that I have added:
let cruiseFilterData = [{
"name":"text--1",
"availabilities":{
"2019":{
"year":"2019",
"items":[
{
"discount":{
"name":"one"
},
"dateDepart":"12\/10\/2019"
},
{
"discount":{
"name":"Two"
},
"dateDepart":"15\/12\/2019"
}
]
}
},
"sort":0,
"featured":true
},
{
"name":"text--2",
"availabilities":{
"2019":{
"year":"2019",
"items":[
{
"discount":{
"name":"one"
},
"dateDepart":"18\/05\/2019"
},
{
"discount":{
"name":"two"
},
"dateDepart":"12\/10\/2019"
}
]
}
},
"sort":1,
"featured":true
},
{
"name":"text--3",
"availabilities":{
"2019":{
"year":"2019",
"items":[
{
"discount":{
"name":"one"
},
"dateDepart":"25\/05\/2019"
},
{
"discount":{
"name":"two"
},
"dateDepart":"12\/10\/2019"
}
]
}
},
"sort":2,
"featured":true
},
{
"name":"text--4",
"availabilities":null,
"sort":2,
"featured":true
}
]
let compareDate = 'Sat Oct 12 2019 00:00:00 GMT+0400 (Mauritius Standard Time)';
const linkedItinerary = cruiseFilterData.map((element) => {
element.availabilities = Object.keys(element.availabilities).filter((value, index) => {
element.availabilities[value].items.filter((value, index) => {
const dateValue = value.dateDepart;
if (dateValue.indexOf('/') >= 0) {
const dateSplit = dateValue.split(' ')[0];
const parts = dateSplit.split('/');
let propDate;
if (parts) {
const dateMerge = '' + parts[1] + '/' + parts[0] + '/' + parts[2];
propDate = new Date(dateMerge);
} else {
propDate = new Date(dateValue);
}
}
if(propDate.toDateString() === new Date(compareDate).toDateString()) {
return value;
}
});
return value;
});
return element;
});
console.log(linkedItinerary);