• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

Transform array of objects with while and reduce

April 11, 2020 by

Questions › Transform array of objects with while and reduce
0
Vote Up
Vote Down
Garmaine asked 3 years ago

I need help with transforming an array of objects. So far I have managed to get all the dates between the initial (begin) and the end (end).

This is what I receive and try to transform.

 const fetchedOffers = [{
    _id: 'dsd87878',
    professional: 'd2ycdd',
    specialty: '704505',
    begin: new Date('2020-04-10T07:00:00.169Z'),
    end: new Date('2020-04-11T21:00:00.169Z'),
    interval: 1200000,
  },
  {
    _id: 'ddd67878',
    professional: '25c0a8',
    specialty: '704505',
    begin: new Date('2020-06-29T08:00:00.169Z'),
    end: new Date('2020-06-30T12:00:00.169Z'),
    interval: 1200000,
  }
];

const hours = fetchedOffers.reduce((prev, acc) => {
  let currentDate = acc.begin.getTime()
  const endDate = acc.end.getTime()

  while (currentDate < endDate) {
    const d = new Date(currentDate)
    if (d.getHours() > 7 && d.getHours() < 20 && d.getDay() < 6 && d.getDay() > 0) {
      prev.push({
        date: new Date(d).toLocaleString('es-CL')
      })
    }
    currentDate = currentDate + acc.interval
  }
  return prev
}, [])
  console.log(hours)

The idea is that you can generate an array of objects like this:

    [
      {
        professional: 'd2ycdd',
        specialty: '704505',
        date: [
          {
            offer: 'dsd87878',
            date: '4/10/2020',
            hours: ['07:00', '07:20', '07:40', '08:00',...]
          },
          {
            offer: 'dsd87878',
            date: '4/11/2020',
            hours: [..., '20:00', '20:20', '20:40', '21:00']
          }
        ]
      },
      {
        professional: '25c0a8',
        specialty: '704505',
        date: [
          {
            offer: 'ddd67878',
            date: '6/29/2020',
            hours: ['08:00', '08:20', '08:40', '09:00',...]
          },
          {
            offer: 'ddd67878',
            date: '6/30/2020',
            hours: [..., '11:00', '11:20', '11:40', '12:00']
          }
        ]
      },
    ]

I want to group by professional id and by date (no time). Each date is obtained starting with the beginning date (begin) and adding the interval until reaching the end date. In addition I have added other conditions when displaying the hours, for example omitting hours between 20 and 7 hours and also only from Monday to Friday.

How can I do that? So far I only get all the dates between the start and end date but I need to group by dates and professional

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: arrays, javascript, object, reduce

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements architecture beautiful life best building calling city commercial convenience employment Finances Cognitive decline Future gadgets Hidden Gems highway Home houses hydration Impact Innovations lamp lighting Mental health military tech Must-See New York City occupation Productivity recreation romance sepia shopping sippy cups smartphones social Technological breakthroughs technology toddlers Treasures turns Uncover Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements architecture beautiful life best building calling city commercial convenience employment Finances Cognitive decline Future gadgets Hidden Gems highway Home houses hydration Impact Innovations lamp lighting Mental health military tech Must-See New York City occupation Productivity recreation romance sepia shopping sippy cups smartphones social Technological breakthroughs technology toddlers Treasures turns Uncover Well-being Wonders Work Young onset dementia

Copyright © 2023