I want to create a function where Item_date will be received from my Database and will return a number of Schedule Dates that are matching with other Item_date.
For instance, I can select 2021-05-20, and the function will return :
19th November: 2 Scheduled
18th November: 2 Scheduled
Can anyone give me an idea of how can I make it?
This is my database example:
[
{
"schedule_time": "2021-05-17 12:39:29",
"slot": "L",
"item_date": "2021-05-18"
},
{
"schedule_time": "2021-05-17 12:47:53",
"slot": "D",
"item_date": "2021-05-18"
},
{
"schedule_time": "2021-05-18 13:55:22",
"slot": "D",
"item_date": "2021-05-19"
},
{
"schedule_time": "2021-05-19 16:09:15",
"slot": "L",
"item_date": "2021-05-20"
},
{
"schedule_time": "2021-05-19 16:11:55",
"slot": "L",
"item_date": "2021-05-20"
},...
]
CodePudding user response:
if you want the other dates you need to do this suppose array is the array where your database info are , and the dbItemDate the parameter passed to javascript :
let dbItemDate="2021-05-20";
let otherDayArray= array.filter(num=>num.item_date!=dbItemDate);
console.log(otherDayArray);//this is the new array of others date
let count = otherDayArray.reduce(function(acc,num){
acc[num.item_date]=(acc[num.item_date]||0) 1;
return acc;
},[]);
console.log(count);//this should print the frequency of each date in the others date array
and if you want the same date just replace the condition in array.filter to
let otherDayArray= array.filter(num=>num.item_date===dbItemDate);
