JavaScript How to create an hours + minutes array with moment.js and ES6?

I'm trying to create an array of hours in a day with 30 minute intervals with moment.js and ES6.

Example: let hours = ["12:00 AM", "12:30 AM", "1:00 AM", "1:30 AM", ..., "11:30 PM"]

I already have this for function:

someFunction () {
  const items = []
  for (let hour = 0; hour < 24; hour++) {
    items.push(moment({ hour }).format('h:mm A'))
    items.push(moment({ hour, minute: 30 }).format('h:mm A'))
  }
  return items
}

But I would like to make it more ES6-like.

I have gotten this far:

someFunction () {
  let timeSlots = new Array(24).fill().map((acc, index) => {
    let items = []
    items.push(moment( index ).format('h:mm A'))
    items.push(moment({ index, minute: 30 }).format('h:mm A'))
  })
  return timeSlots
}

But it outputs:

["1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", ...]

Answer:1

You could use Array.from method to generate 2d array and then concat with spread syntax to create 1d array.

function create () {
  return [].concat(...Array.from(Array(24), (_, hour) => ([
    moment({hour}).format('h:mm A'),
    moment({ hour, minute: 30 }).format('h:mm A')
  ])))
}

console.log(create())
<script src="http://momentjs.com/downloads/moment.js"></script>
Answer:2

You can use array#from with array#reduce to generate 30 minutes interval time.

let someFunction = () => {
  return Array.from({length: 24}, (_,i) => i).reduce((r,hour) => {
     r.push(moment({hour, minute: 0}).format('h:mm A'));
     r.push(moment({hour, minute: 30}).format('h:mm A'));
     return r;
  }, []);
}
console.log(someFunction());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Answer:3

I have an automatically playing slideshow (controlled by jQuery/JS), that requires each image to be absolutely positioned. The problem is that I'm trying to horizontally centre this slideshow on my ...

I have an automatically playing slideshow (controlled by jQuery/JS), that requires each image to be absolutely positioned. The problem is that I'm trying to horizontally centre this slideshow on my ...

I am trying to catch an element using an ID in React, but I could not. render() { //Looping through all menus let menuOptions = this.props.menuLists.map(menuList => { return ( <li ...

I am trying to catch an element using an ID in React, but I could not. render() { //Looping through all menus let menuOptions = this.props.menuLists.map(menuList => { return ( <li ...

  1. object inside react component

I'd like to download a protected file from my backed - I have to send authorization headers, so I can't link it directly. I have created following Ajax request to download it: Ember.$.ajax({ url:...

I'd like to download a protected file from my backed - I have to send authorization headers, so I can't link it directly. I have created following Ajax request to download it: Ember.$.ajax({ url:...

  1. javascript create blob from file
  2. javascript create blob from string
  3. javascript create blob from url
  4. javascript create blob from base64
  5. javascript create blob from pdf url
  6. javascript create blob from arraybuffer
  7. javascript create blob from image url
  8. javascript create blob from video url
  9. javascript create blob from byte array
  10. javascript create blob from pdf
  11. javascript create blob from file path
  12. javascript create blob with filename
  13. javascript create blob from image
  14. javascript create blob from response
  15. javascript create blob from json
  16. javascript create blob from uint8array
  17. javascript create blob from data url
  18. javascript create blob from binary data

I'm looking for an operator which transform the stream to the stream with history or last N events. So for a stream e.g. like this one: clicks$.subscribe((ev) => ...) I would like to have ...

I'm looking for an operator which transform the stream to the stream with history or last N events. So for a stream e.g. like this one: clicks$.subscribe((ev) => ...) I would like to have ...

  1. rxjs which operator to use
  2. rxjs which operator do i use
  3. rxjs which operator
  4. rxjs operator decision tree
  5. rxjs operator map
  6. rxjs operator tap
  7. rxjs operator do
  8. rxjs operator function
  9. rxjs operator filter
  10. rxjs operator first
  11. rxjs operator take
  12. rxjs operator delay
  13. rxjs operator pipe
  14. rxjs operator list
  15. rxjs operator switchmap
  16. rxjs operator complete
  17. rxjs operator catcherror
  18. rxjs operator timeout
  19. rxjs operator share
  20. rxjs operator merge