0

I have this piece of code that gets appointment times and dates from a http request :

 $scope.contents = data;
        angular.forEach($scope.contents, function(item){
                          item.Appointment = moment(item.Appointment).format('MM/DD/YYYY'); 
                          var d = moment(item.Appointment).format('MM/DD/YYYY');
                          var t = item.txtTime;
                          appointmentsArr.push(d + " " + t);
                       })

My result is :

[ '03/05/2016 4:29:00 PM',
  '04/05/2016 11:23:00 AM',
  '04/05/2016 11:22:00 AM',
  '05/04/2016 10:52:00 AM',
  '05/03/2016 9:36:00 AM',
  '04/29/2016 12:53:00 PM',
  '04/05/2016 11:19:00 AM',
  '04/30/2016 12:54:00 AM',
  '01/01/1900 12:00:00 AM',
  '04/30/2016 8:52:00 AM' ]

My question is, is it possible to kind of group this data so that i can check if an appointment exists already? i want to bring up an error if there is an appointment within an hour of the time on that date..

so would the dates be an array with the times inside? i imaging it would be something like the below sudo?

[ [03/05/2016] 4:29:00 PM',
  [04/05/2016] '11:23:00 AM,11:22:00 AM,12:53:00 PM'
  [05/04/2016] 10:52:00 AM ] 

How could i achieve this?

3
  • You need to group the Main array based on what condition?? Commented May 4, 2016 at 10:42
  • date, so when i make a new appointment i can check if time is available for that date Commented May 4, 2016 at 10:43
  • Inside your .foreach just add logic that will look for an item date (already have it), group all appointments that are on a given item date and using momentjs just check if a given item is somewhere within 1 hour range of any appointment. Commented May 4, 2016 at 10:46

1 Answer 1

1

You need to push to an array with date as the key.

$scope.contents = data;
angular.forEach($scope.contents, function(item){
  var d = moment(item.Appointment).format('MM/DD/YYYY');
  var t = item.txtTime;

  if(!appoinmentObj[d]){
    appoinmentObj[d] = [];
  }
  appoinmentObj[d].push(t);
});
console.log(appoinmentObj);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.