title: Always getting last value from array while in loop
body: <p>I have array which contains 1 sub array. From that array I have to make format like:</p>
  
  <pre><code>    '2012-05-08': {
          dots: [{
              key: 'vacation',
              color: 'blue',
              selectedDotColor: 'white'
          }, {
              key: 'massage',
              color: 'red',
              selectedDotColor: 'white'
          }],
          selected: true
      },
  </code></pre>
  
  <p>So I used reduce method and get all dates as a key. Now the object which I am getting is for some particular date like:
  [{…}, {…}]</p>
  
  <p>And final result I am getting is this:
  2019-05-08: {dots: Array(1)} which is just taking the last object in an array.</p>
  
  <p>Actual format I want is:
  2019-05-08: {dots: Array(2)} . // length of sub array </p>
  
  <p>I have tried using reduce, map , forEach, but all of them return last value of the array</p>
  
  <pre><code>let temp = responseJson.data.reduce((previousValue, currentValue) =&gt; {
            console.log('current val.... ', currentValue);
            let key = moment(currentValue.event_date).format('YYYY-MM-DD');
  
              previousValue[key] = currentValue.events;
  
  
            return previousValue;
          }, {});
  </code></pre>
  
  <p>Output of temp:
  2019-04-16: (2) [{…}, {…}]
  ...</p>
  
  <pre><code>eventDates = responseJson.data.map(({ event_date} ) =&gt; moment(event_date).format('YYYY-MM-DD'));
  // array of date string
  </code></pre>
  
  <pre><code>if(eventDates.length) {
            let eventRecord;
  
            eventDates.map((date) =&gt; {
              eventRecord = temp[date];
              this.setState({ eventData1 : eventRecord.reduce((previousValue, currentValue) =&gt; {
                previousValue[date] = {
                  dots: [{
                      key:currentValue._id,
                      color: 'black'
                  }]
                }
  
                return previousValue
              }, {}) }, () =&gt; console.log('states... ', this.state.eventData1))
  
              })
          }
  </code></pre>
  
  <p>This is the response json:</p>
  
  <pre><code>carerId: "5cad711296b53f1d16b137d0"
  event_date: "2019-04-17T12:32:36.131Z"
  events: (2) [{…}, {…}]
  grandpaId: "5cad8a5cb5b125269edd816b"
  </code></pre>