3

I have a "format" array that's used in a map function to return an array of objects with start and end dates.

This format array contains the group of dates that belong to the same object.

let format = [3, 3, 1, 5, 4, 4, 3, 5, 13, 10, 3, 5, 5, 2, 2, 10];

So this way we know the first 3 dates same group, next 3 dates, same group, next date single group date, etc.

My issue/problem: is when there's a group of dates with no-lineal dates (for example 05, 06, 12, 13).

With my actual function, is returning an object with

Start: 05 End: 13

But this isn't correct, because we are counting all the days in the middle between 5 and 13. What I would like to do would create two objects for this case:

{
  "start": 05,
  "end": 06
},
{
  "start": 12,
  "end": 13
}

In my code, you can see this behavior with the last group of dates (10) (last object).

Is there any way to "check" for the range-dates" before creating the object? Should I add a .map inside the current .map to get 3 sets from the last 10

{
"2022-01-05T04:00:00.000Z", // start
"2022-01-06T04:00:00.000Z",
"2022-01-07T04:00:00.000Z" // end
},
{
"2022-01-10T04:00:00.000Z", // start
"2022-01-11T04:00:00.000Z",
"2022-01-12T04:00:00.000Z",
"2022-01-13T04:00:00.000Z",
"2022-01-14T04:00:00.000Z" // end
},
{
"2022-01-17T04:00:00.000Z", // start
"2022-01-18T04:00:00.000Z"  // end
}

Current code:

let format = [3, 3, 1, 5, 4, 4, 3, 5, 13, 10, 3, 5, 5, 2, 2, 10];
let dates = [
  "2021-10-04T04:00:00.000Z",
  "2021-10-06T04:00:00.000Z",
  "2021-10-07T04:00:00.000Z",
  "2021-10-13T04:00:00.000Z",
  "2021-10-14T04:00:00.000Z",
  "2021-10-15T04:00:00.000Z",
  "2021-10-15T04:00:00.000Z",
  "2021-10-17T22:00:00.000Z",
  "2021-10-18T22:00:00.000Z",
  "2021-10-19T22:00:00.000Z",
  "2021-10-20T22:00:00.000Z",
  "2021-10-21T22:00:00.000Z",
  "2021-10-17T22:00:00.000Z",
  "2021-10-18T22:00:00.000Z",
  "2021-10-19T22:00:00.000Z",
  "2021-10-20T22:00:00.000Z",
  "2021-10-19T04:00:00.000Z",
  "2021-10-20T04:00:00.000Z",
  "2021-10-21T04:00:00.000Z",
  "2021-10-22T04:00:00.000Z",
  "2021-10-19T04:00:00.000Z",
  "2021-10-20T04:00:00.000Z",
  "2021-10-21T04:00:00.000Z",
  "2021-10-25T04:00:00.000Z",
  "2021-10-26T04:00:00.000Z",
  "2021-10-27T04:00:00.000Z",
  "2021-10-28T04:00:00.000Z",
  "2021-10-29T04:00:00.000Z",
  "2021-10-25T04:00:00.000Z",
  "2021-10-26T04:00:00.000Z",
  "2021-10-27T04:00:00.000Z",
  "2021-10-28T04:00:00.000Z",
  "2021-10-29T04:00:00.000Z",
  "2021-11-01T04:00:00.000Z",
  "2021-11-02T04:00:00.000Z",
  "2021-11-03T04:00:00.000Z",
  "2021-11-04T04:00:00.000Z",
  "2021-11-05T04:00:00.000Z",
  "2021-11-08T04:00:00.000Z",
  "2021-11-09T04:00:00.000Z",
  "2021-11-10T04:00:00.000Z",
  "2021-11-01T04:00:00.000Z",
  "2021-11-02T04:00:00.000Z",
  "2021-11-03T04:00:00.000Z",
  "2021-11-04T04:00:00.000Z",
  "2021-11-05T04:00:00.000Z",
  "2021-11-08T04:00:00.000Z",
  "2021-11-09T04:00:00.000Z",
  "2021-11-10T04:00:00.000Z",
  "2021-11-11T04:00:00.000Z",
  "2021-11-12T04:00:00.000Z",
  "2021-11-11T04:00:00.000Z",
  "2021-11-12T04:00:00.000Z",
  "2021-11-13T04:00:00.000Z",
  "2021-11-15T04:00:00.000Z",
  "2021-11-16T04:00:00.000Z",
  "2021-11-17T04:00:00.000Z",
  "2021-11-18T04:00:00.000Z",
  "2021-11-19T04:00:00.000Z",
  "2021-11-16T04:00:00.000Z",
  "2021-11-17T04:00:00.000Z",
  "2021-11-18T04:00:00.000Z",
  "2021-11-19T04:00:00.000Z",
  "2021-11-20T04:00:00.000Z",
  "2021-11-23T04:00:00.000Z",
  "2021-11-24T04:00:00.000Z",
  "2021-11-23T04:00:00.000Z",
  "2021-11-24T04:00:00.000Z",
  "2022-01-05T04:00:00.000Z",
  "2022-01-06T04:00:00.000Z",
  "2022-01-07T04:00:00.000Z",
  "2022-01-10T04:00:00.000Z",
  "2022-01-11T04:00:00.000Z",
  "2022-01-12T04:00:00.000Z",
  "2022-01-13T04:00:00.000Z",
  "2022-01-14T04:00:00.000Z",
  "2022-01-17T04:00:00.000Z",
  "2022-01-18T04:00:00.000Z"
];

const res = format.map(num => {
  const arr = dates.splice(0,num)
  const start = arr.shift();
  const end = arr.length === 0 ? start : arr.pop()
  return { 
    start: start,
    end: end 
  }
})

console.log(res);
davs0ft
  • 43
  • 3
  • I suspect this would be a lot easier to do with an array of objects containing properties for date and format, but in any case, have you tried it without the leading 0? prefixing a number value in javascript with 0 tells it it's an octal https://stackoverflow.com/questions/35047982/javascript-0-in-beginning-of-number – coppereyecat Oct 28 '21 at 15:14

0 Answers0