[Vuejs]-Nested loop involving date manipulation in Javascript

0👍

Is this what you’re trying to do?

window['moment-range'].extendMoment(moment);

var seasonsObject = {
  low: {
    period: "low",
    seasonStart: "2017-11-01T12:01:00.000Z",
    seasonEnd: "2018-03-15T12:01:00.000Z"
  },
  high: {
    period: "high",
    seasonStart: "2018-06-01T12:01:00.000Z",
    seasonEnd: "2018-08-01T12:01:00.000Z"
  }
}


function makeFutureSeasons(count) {
  const futureSeasons = {};
  _.range(0, count).forEach(y => {
    Object.keys(seasonsObject).forEach(k => {
      const {
        period,
        seasonStart,
        seasonEnd
      } = seasonsObject[k];

      futureSeasons[k + y] = {
        period,
        seasonStart: moment(seasonStart).add(y, 'years').toISOString(),
        seasonEnd: moment(seasonEnd).add(y, 'years').toISOString()
      };
    });
  });
  return futureSeasons;
}

const fseasons = makeFutureSeasons(5);
console.log(fseasons);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-range/3.1.1/moment-range.min.js"></script>

Leave a comment