How to organize a javascript object that contains store hours?


If I have an object with store information that looks like this.

storeHours: 0: day: Sunday Open: 9:00am close: 6:00pm 0: day: Monday Open: 8:00am close: 10:00pm 0: day: Tuesday Open: 8:00am close: 10:00pm 0: day: Wednesday Open: 8:00am close: 10:00pm 0: day: Thursday Open: 8:00am close: 10:00pm 0: day: Friday Open: 8:00am close: 10:00pm 0: day: Saturday Open: 9:00am close: 6:00pm

What function should I write to get output that would give me

simplestoreHours: 0: Days: Monday-Friday open: 8:00am close: 6:00pm 1: Days: Saturday-Sunday open: 9:00am close: 6:00pm

Also all the times are formatted momentjs objects so it is possible to use momentjs.

My initial thought is to do a loop and have each element checked against each other. If the open and closing times are the same then just connect the days, but I don't think this very clever.


reduce Array method can help you.

<ol><li>we have to iterate over initial dates object.</li> <li>group days and working hours into 2 helper arrays by key. i used concatenated open and close time</li> <li>keep open and close time in ranges sub array </li> <li>as we are not sure initial array sorted by day of week i've added day index into days grouped array.</li> <li>

on last iteration lets come through ranges we have and find days with range key, sort it, create Days property as concationation first and last sorted days array and save combined object.

var groupedObject = obj.reduce(function (memo, item, i, array) { var key = item.open+item.close; memo.ranges[key] = memo.ranges[key] || {}; memo.days[key] = memo.days[key] || []; memo.ranges[key] = { open: item.open, close: item.open }; memo.days[key].push({ index: ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'].indexOf(item.day), name: item.day }) if (i == array.length-1 ) { for (var key in memo.ranges) { if (memo.ranges.hasOwnProperty(key)) { memo.days[key].sort(function(a,b) { return a.index - b.index }); memo.grouped.push({ Days: [ memo.days[key][0].name, memo.days[key][memo.days[key].length-1 ].name ].join('-'), open: memo.ranges[key].open, close: memo.ranges[key].close }); } } } return memo }, { ranges: {}, days: {}, grouped: [] }); console.log(groupedObject.grouped); </li> </ol>

thats it)


You could do it this way:

var storeHours = [ {day: 'Sunday', open: '9:00am', close: '6:00pm' }, {day: 'Monday', open: '8:00am', close: '10:00pm' }, {day: 'Tuesday', open: '8:00am', close: '10:00pm' }, {day: 'Wednesday', open: '8:00am', close: '10:00pm' }, {day: 'Thursday', open: '8:00am', close: '10:00pm' }, {day: 'Friday', open: '8:00am', close: '10:00pm' }, {day: 'Saturday', open: '9:00am', close: '6:00pm' } ]; function sort() { var daysSorted = []; for (var dayCounter = 0, length = storeHours.length; dayCounter < length; dayCounter++) { var open = storeHours[dayCounter].open, close = storeHours[dayCounter].close, name = storeHours[dayCounter].day, found = false; for (var sortedDayCounter = 0, lengthSorted = daysSorted.length; sortedDayCounter < lengthSorted; sortedDayCounter++) { var sortedOpen = daysSorted[sortedDayCounter].open, sortedClose = daysSorted[sortedDayCounter].close; if (sortedOpen === open && sortedClose == close) { daysSorted[sortedDayCounter].day += ', ' + name; found = true; } }; if (!found) { daysSorted.push(storeHours[dayCounter]); } }; return daysSorted; } console.log(sort());

Anyway, I recommend that you consider open and close dates as Date types, rather than strings.

Hope this helps,


  • Custom calendar with bootstrap and momentjs
  • moment.js thinks that 2013-12-31 is week 1, not week 53
  • toastr undefined with requirejs
  • spark udf with data frame
  • How to parse given date string using moment.js?
  • Find text(A) and replace it with text(B) + HTML
  • How does the heap manager in java or C++ keep track of all the memory locations used by the threads
  • Refreshing i18n translated string interpolated values in Aurelia
  • Simplifying the use of meshgrid in Matlab
  • How to insert an Image in WORD after a bookmark using OpenXML
  • Hadoop shuffle uses which protocol?
  • how to UNSELECT row in a ttk.Treeview in tkinter
  • finding greatest prime factor using recursion in c
  • True privateness in Python
  • Imageloader not loading image on real device
  • Dependable views in Ember
  • Avoid registering duplicate broadcast receivers in Android
  • Java making confirming exit
  • XSLT foreach repeating nodes to flat
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • Do query loads all the data in memory
  • How to make R's read_csv2() recognise the text characters properly
  • Calculate time difference in hh:mm:ss with simple javascript/jquery
  • How to access culture data in globalize.js V1.0.0
  • Dynamically switching connect in Modelica
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Redux Form - Not able to type anything in input
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • Debug.DrawLine not showing in the GameView
  • print() is showing quotation marks in results
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • CSS Linear-gradient formatting issue accross different browsers
  • Deselecting radio buttons while keeping the View Model in synch
  • Nant, Vault & Windows Integrated Authentication
  • Weird JavaScript statement, what does it mean?
  • Django query for large number of relationships
  • Python/Django TangoWithDjango Models and Databases