API
API

Reputation: 47

JS: Map the inside of Array to a range of two dates

I have calendar like this:

const calendar = [{
  "Title": "Go Shopping",
  "dates": [ 
  {
  "DateBeginn": "19.02.2021",
  "DateEnd": "22.02.2021",
  "Where": "Supermarket",
  },
  {
  "DateBeginn": "23.02.2021",
  "DateEnd": "23.02.2021",
  "Where": "Biomarket"
  }]
  }]
  
 const calendarData = calendar.map(main => main.dates.map(d => ({
  Title: main.Title,
  DateBeginn: d.DateBeginn,
  DateEnd: d.DateEnd,
  Where: d.Where,
  
 }))).flat()
  console.log(calendarData)

This puts something like this out:

  [
      {
        "Title": "Go Shopping",
        "DateBeginn": "19.02.2021",
        "DateEnd": "22.02.2021",
        "Where": "Supermarket"
      },
      {
        "Title": "Go Shopping",
        "DateBeginn": "23.02.2021",
        "DateEnd": "23.02.2021",
        "Where": "Biomarket"
      }
    ]

Now what i would like to have is something like this:

[
  {
    "Title": "Go Shopping",
    "DateBeginn": "19.02.2021",
    "DateEnd": "22.02.2021",
    "Where": "Supermarket"
  },
  {
    "Title": "Go Shopping",
    "DateBeginn": "20.02.2021",
    "DateEnd": "22.02.2021",
    "Where": "Supermarket"
  },
  {
    "Title": "Go Shopping",
    "DateBeginn": "21.02.2021",
    "DateEnd": "22.02.2021",
    "Where": "Supermarket"
  },
  {
    "Title": "Go Shopping",
    "DateBeginn": "22.02.2021",
    "DateEnd": "22.02.2021",
    "Where": "Supermarket"
  },
  {
    "Title": "Go Shopping",
    "DateBeginn": "23.02.2021",
    "DateEnd": "23.02.2021",
    "Where": "Biomarket"
  }
]

That means i would like to map the inside of dates[{}] to a range of two dates if the difference is bigger than 1 day. In this example: Get the date range from 19.02.2021 to 22.02.2021 get every date and then create a new entity for each date. Then i would like to map the contents of the date array to each entity

Upvotes: 1

Views: 183

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370629

You can concisely add days and format them with Moment, and push the formatted strings to an array. Once the formatted string is equal to the DateEnd, you can map and return the array of day strings:

const calendar = [{
    "Title": "Go Shopping",
    "dates": [{
        "DateBeginn": "19.02.2021",
        "DateEnd": "22.02.2021",
        "Where": "Supermarket",
    },
    {
        "DateBeginn": "23.02.2021",
        "DateEnd": "23.02.2021",
        "Where": "Biomarket"
    }
    ]
}]

const format = 'DD.MM.YYYY';
const calendarData = calendar.flatMap(main => main.dates.flatMap(d => {
    const momentObj = moment(d.DateBeginn, format);
    const endDateStr = d.DateEnd;
    const dateStrs = [d.DateBeginn];
    while (momentObj.format(format) !== endDateStr) {
        momentObj.add(1, 'days');
        dateStrs.push(momentObj.format(format));
    }
    return dateStrs.map(dateStr => ({
        Title: main.Title,
        DateBeginn: dateStr,
        DateEnd: dateStr,
        Where: d.Where,
    }));
}));
console.log(calendarData)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Upvotes: 1

Related Questions