i'm trying to filter a JSON file with objects that have a property with Dates. I have it to filter this JSON, separate the content by a property that have a Date and by Day. After that i have to get this "parts" and filter again... but the most problem is to create this new agrouped by Day parts to filter again...
The code to examplify:
// this is the Data from the API just to simulate
const data =
[
{ date: "10/01/2023", id: 1 , whatever: "hi" },
{ date: "01/02/2023", id: 2 , whatever: "hello"},
]
// this is the allDates filtered that i get before from the JSON with no nulls and no duplicates
const dtList = ["10/01/2023", "01/02/2023"];
so, for now i have this example, that i used before(that i found here) to filter dates by month, but it only works with months:
var months = [
"Jan",
"Feb",
"Mar",
"Ap",
"May",
"Jun"
];
var ordered = {};
// this data come from the api for example
for (var i = 0, len = data.length; i < len; i++) {
var entry = data[i];
var m = entry.date.substring(0, 10);
if (m !== "") {
var mm = parseInt(m.split("/")[1]) - 1;
if (!ordered[months[mm]]) {
ordered[months[mm]] = [];
}
ordered[months[mm]].push(entry);
}
}
this code creates an object that contains all the data filtered and separated with the months, so after that i can do that:
//i filtered again with all the things i want and by date because ordered have subobjects separated by month...
ordered.Jan?.forEach((obj) => {
if (!isNaN(obj.nuParc)) {
nuParcJun += obj.nuParc;
setNuParcJun(nuParcJun);
}
// this code have some react too
and i need to do the same thing but with days.
//i was trying something near to this, but with no results untill now
dtList.forEach((value) => {
var dtsSep = Data.filter((obj) =>
obj.date.includes(value)
);
var ordered = {};
var ordered1 = Object.assign(ordered, dtsSep);
You have to parse the date string to an actual 'Date' object, then group the data and then perform your desired operation on each group like this:
hope that helps ✌️