0

Trying to create a compound object in map but stuck at a point. map over an array which has start and end time and some event. below is code.

let detailsRes = Object.create({});

response.map((res, i) => {
    let start = new Date(moment(res.startTime).format('L'));
    let end = new Date(moment(res.endTime).format('L'));
    let current = new Date(start);
    while (current <= end) {
      date = [new Date(current).getFullYear(), new Date(current).getMonth() + 1, new Date(current).getDate()].join('-');
      detailsRes[date] == undefined ? (detailsRes[date] = 1) : (detailsRes[date] += 1);
      var newDate = current.setDate(current.getDate() + 1);
      current = new Date(newDate);
    }
  });

sample data

[{
    "endTime": "2020-12-06 16:30:00.000Z",
    "event": "Study",
    "startTime": "2020-12-06 12:30:00.000Z",
    'somedata':'js is good'
  },
  {
    "endTime": "2020-12-06 16:30:00.000Z",
    "event": "gym",
    "startTime": "2020-12-05 12:30:00.000Z",
    'somedata':'js is good'
  },
]

current output

  {5-12-2020 : 1, 6-12-2020 : 2 },

expected output

5-12-2020 : {
    totalcount: 1,  // 5 is only 1 time
    event1: 1 //gym
  },
  6-12-2020 : {
    totalcount:2,  // 2 times 6-12 is there so count is 2
    event1: 1, //gym
    event2: 1 // study
  }

so basically checking how many events are coming between dates, if event is between 1-12 to 3-12 then individual object of 1,2,3 and totalcount of 1 for each date and if any other events coming in between then increment that particular totalcounts. above given output of mine which is correct but trying to modify object like expected output. so i need this kind of object.

8
  • What if your event spans across multiple days (e.g. 5-th through 8-th of December) should the entries for missing days (i.e. 7-12-2020) be created? Commented Dec 4, 2020 at 14:36
  • I dont follow what event1:1 and event2:1 refer to? is there just a new key eventN for every event always with the value 1? Can there be event1:2 if so how? Commented Dec 4, 2020 at 14:38
  • @YevgenGorbunkov yes, whatever the dates available, it will count it. Commented Dec 4, 2020 at 14:39
  • @Jamiec as i mentioned if date is coming multiple times, then it count that event, e.g date - 6-12 is in first event which is 5-12 to 6-12 and 6-12 is also event. so it will combine those 2 events and give count. Commented Dec 4, 2020 at 14:42
  • @DharmikSoni still not clear im afraid. See if my answer does what you want. If not clarify Commented Dec 4, 2020 at 14:44

1 Answer 1

1

Your requirements are not exactly clear, but what is for sure is you should be using reduce not map for this.

Making the result an object is easy, see below, but I'm still not entirely clear how you build the eventN keys correctly. This certainly works for your test case presented here, if it doesn't work for what you want feel free to clarify and I will update as necessary.

var response = [{
    "endTime": "2020-12-06 16:30:00.000Z",
    "event": "Study",
    "startTime": "2020-12-06 12:30:00.000Z",
    'somedata':'js is good'
  },
  {
    "endTime": "2020-12-06 16:30:00.000Z",
    "event": "gym",
    "startTime": "2020-12-05 12:30:00.000Z",
    'somedata':'js is good'
  },
]

let detailsRes = response.reduce((acc, res) => {
    let start = new Date(moment(res.startTime).format('L'));
    let end = new Date(moment(res.endTime).format('L'));
    let current = new Date(start);
    var n = 0;
    while (current <= end) {
      date = [new Date(current).getFullYear(), new Date(current).getMonth() + 1, new Date(current).getDate()].join('-');
      if(acc[date] == undefined) acc[date] = {totalcount:0}
      acc[date].totalcount++;
      acc[date]["event" + ++n] = 1;
      var newDate = current.setDate(current.getDate() + 1);
      current = new Date(newDate);
    }
    return acc
  }, {});
  
console.log(detailsRes)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

this will work for me, thanks for quick help. Upvote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.