JS filter array or arrays with unknown length?

I have some data I’m trying to transform by filtering by date, however the data can vary in length i.e like this;

longData = [
     // These objects could be unlimited but the name key will always be unique
    {name: Opt 1,
     Data:
         // The first value will always be a date string and the second is the amount, the length of this array is always unknown
         ['2021-01-02', 30],
         ['2021-01-03, 20],
         ['2021-02-05', 15]
     },
     {name: Opt 2,
     Data:
         ['2021-01-02', 30],
         ['2021-02-08, 20],
         ['2021-04-02, 15]
     },
     {name: Opt 3,
     Data:
         ['2021-03-02', 30],
         ['2021-03-04, 20]
     }

What I want to do is essentially create a new object that has the same shape and the original but only includes the data between my dates.

I.e if I have a start date of ‘2021-01-01’ and end of date ‘2021-01-07’

I should get an object like the following..

shortData = [
    {name: Opt 1,
     Data:
         ['2021-01-02', 30],
         ['2021-01-03, 20]
     },
     {name: Opt 2,
     Data:
         ['2021-01-02', 30]
     },
     {name: Opt 3,
     Data:
         []
     }

I’m trying to break it down by week, these numbers are rendered in a chart and when dealing with large data sets it can break the chart essentially, so I’m trying to page it by week.

Any thoughts on how to accomplish it would be rather helpful.

19 thoughts on “JS filter array or arrays with unknown length?”

Leave a Comment