0

I have an array called open and within that I have arrays for each day indexing from 0 - 6. Within each day array I have times and a day set. Within some of the days the times will be the same and therefore want to group the values that are the same. e.g

open[] ->    [0] -> {open: 12:00, close: 16:00, Monday}
             [1] -> {open: 12:00, close: 16:00, tuesday}

How could i group these value so that I can have an array that has the time and all the days associated with it. OR The best way to display that these days have this time.

this open array can also contain times that arent the same e.g:

open[] ->    [0] -> {open: 12:00, close: 16:00, monday}
             [1] -> {open: 12:00, close: 16:00, tuesday}
             [1] -> {open: 14:00, close: 16:00, sunday}

Any help is appreciated!

  • Hey :) What have you already tried yourself to do this? Please review [How much research effort is expected?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). Stack Overflow is not a coding service. You are expected to research your issue and make a good attempt to write the code yourself before posting. If you get stuck on something specific, come back and include a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and a summary of what you tried, so we can help. – cramopy Nov 14 '17 at 15:19
  • @cramopy I've tried looping through each index in the days arr, the first time it adds it to an array, then when it does it the second time it checks whether it exists. If it does add the day otherwise create a new array. Thats when i run into the issue of how do i check the second array... –  Nov 14 '17 at 15:23
  • check this, https://stackoverflow.com/questions/40774697/how-to-group-array-of-objects-by-key – EnriqueDev Nov 14 '17 at 15:24
  • Possible duplicate of [How to group array of objects by key](https://stackoverflow.com/questions/40774697/how-to-group-array-of-objects-by-key) – EnriqueDev Nov 14 '17 at 15:24

1 Answers1

0

I'm not sure if I understand your requirements 100%, but based on what you've said I think you want to group days of the week based on having the same open & close times. To do this, you can create a "key" by concatenating the open & close times, then use this key to lookup the group array in an object. Here's an example:

//Set up an array of randomized test data
var initTestData = function(){
  var testReferenceData = {
    openTimes: ["08:00","08:30","09:00"],
    closeTimes: ["05:00","05:30"],
    daysOfWeek: ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
  }
  var data = [];
  for(var d=0; d<testReferenceData.daysOfWeek.length; ++d){
    var randOpen = Math.floor(Math.random() * testReferenceData.openTimes.length),
        randClose = Math.floor(Math.random() * testReferenceData.closeTimes.length);
    var day = {
      day: testReferenceData.daysOfWeek[d],
      open: testReferenceData.openTimes[randOpen],
      close: testReferenceData.closeTimes[randClose]
    };
    data.push(day);
  }
  return data;
}

//Group days by times
var groupByTime = function(days){
  var groups = {};
  for(var d=0; d<days.length; ++d){
    //Create a key by concatenating the start & end times of a day
    var key = days[d].open+"_"+days[d].close;
    //If this is the first one, initialize an array
    if(!groups[key]){
      groups[key] = [];
    }
    //Add the day to the group by its key
    groups[key].push(days[d]);
  }
  //Return an array of our groups
  return Object.values(groups);
}

var open = initTestData();
console.log("Initial array", open);
var grouped = groupByTime(open);
console.log("Grouped array", grouped);
JstnPwll
  • 8,585
  • 2
  • 33
  • 56