I have been breaking my head the last few days trying to implement the following functionality:
I have an incoming object, with a nested sub-object that's a list of dates. I would like to filter the original object against a range of dates, and return the modified filtered object, with only the specified dates.
Here is the desired functionality:
let incomingObject = {
'one': {
id: "one",
dates: {
"2021-05-01": [{ a: "foo", b: "bar" }],
"2021-05-02": [{ a: "foo", b: "bar" }] } },
'two': {
id: "two",
dates: {
"2021-05-01": [{ a: "foo", b: "bar" }, { a: "foo2", b: "bar2" }],
"2021-05-02": [{ a: "baz", b: "far" }] } },
'three': {
id: "three",
dates: {
"2021-05-03": [{ a: "foo", b: "bar" }],
"2021-05-02": [{ a: "foo", b: "bar" }] } } };
// Function to get an array between two dates
const getDaysArray = function (s, e) {
for (var a = [], d = new Date(s); d <= new Date(e); d.setDate(d.getDate() + 1)) {
a.push(new Date(d));
}
let aStrings = a.map((date) => date.toISOString().slice(0, 10));
return aStrings;
};
I have no idea how to implement this function, that would return the "filtered" object:
filterResults(incomingObject, getDaysArray("2021-05-01", "2021-05-01"));
This is the desired result - all the (sub-)objects that don't pass the filter, are left out:
let desiredResult = {
'one': {
id: "one",
dates: {
"2021-05-01": [{ a: "foo", b: "bar" }] } },
'two': {
id: "two",
dates: {
"2021-05-01": [{ a: "foo", b: "bar" }, { a: "foo2", b: "bar2" }] } } };
And my progress so far:
let dateRange = getDaysArray("2021-05-01", "2021-05-01");
// This logs out only the required keys –– however, logging out the whole "parent" object is completely beyond my comprehension at the moment...
const filteredImages = Object.keys(images).forEach((key) => {
let imgObject = images[key];
Object.keys(images[key].dates).forEach((key) => {
if (dateRange.includes(key)) {
console.log(key);
}
});
});
All help or pointers much appreciated!