Group items from an array in Angularjs

2

I have an array with values of hours which, I present in a tag select as follows:

Select

07:00:00
07:10:00
07:20:00
07:30:00
07:40:00
07:50:00
08:00:00

But I want to know how to group them in the following way:

07:00:00 - 07:10:00
07:10:00 - 07:20:00
07:20:00 - 07:30:00
07:30:00 - 07:40:00
07:40:00 - 07:50:00
07:50:00 - 08:00:00

This is what I have so far:

HTML

<div ng-controller="MyCtrl">
  <select ng-model="data.rh"
          ng-init="data.rh=rangoHoras[0].h"
          ng-options="rh.h as rh.h for rh in rangoHoras"
          style="color:#283593;">
  </select>
</div>

controller.js

function MyCtrl($scope) {
    $scope.rangoHoras=[
        {"h":"07:00:00"},{"h":"07:10:00"},{"h":"07:20:00"},
        {"h":"07:30:00"},{"h":"07:40:00"},{"h":"07:50:00"},
        {"h":"08:00:00"}
    ];
}
    
asked by Dimoreno 06.08.2016 в 02:34
source

1 answer

3

You can make a method that serves as a filter to return the schedules the way you are looking.

$scope.generarRangoHoras =function() {
    var tmp = [];
    for(var i = 0;i<$scope.rangoHoras.length-1; i++){
      tmp.push({
        h:$scope.rangoHoras[i].h+ " - "+ $scope.rangoHoras[i+1].h
      });      
    }
    return tmp;
 }

And the selection is as follows

 <select ng-options="rh.h as rh.h for rh in generarRangoHoras()" ng-model="seleccionado"/>

Example:

link

    
answered by 06.08.2016 / 03:42
source