Data of an Array

Asked

Viewed 350 times

0

I’m having a little bit of a problem here, the logic I should use for this kind of procedure is a bit complicated.

Well, here’s the thing:

I have an array with the following data:

var dados = [
    {'id':1,'nome':'dom','horario1':'01:00','horario2':'02:00'},
    {'id':1,'nome':'seg','horario1':'07:00','horario2':'08:00'},
    {'id':1,'nome':'ter','horario1':'09:00','horario2':'10:00'},
    {'id':1,'nome':'qua','horario1':'01:00','horario2':'02:00'},
]

I want to make the array to be traversed and everyone who has the same time it joins the name, in the example above would look like this:

var Retorno = [
    {'id':1,'nome':'dom,qua','horario1':'01:00','horario2':'02:00'},
    {'id':1,'nome':'seg','horario1':'07:00','horario2':'08:00'},
    {'id':1,'nome':'ter','horario1':'09:00','horario2':'10:00'},
]

There’s a way you can help me Please, I’m getting ugly here!

3 answers

1

An alternative would be to create a filter to display only unique values. Ex:

var myApp = angular.module('myApp',[]);

myApp.filter('unique', function() {
   return function(colecao, campo) {
      var saida = [], 
          keys = [];

      angular.forEach(colecao, function(item) {
          var key = item[campo];
          if(keys.indexOf(key) === -1) {
              keys.push(key);
              saida.push(item);
          }
      });

      return saida;
   };
});

myApp.controller('MyCtrl', function($scope, $filter) {
    $scope.dados = [
    	{'id':1,'nome':'dom','horario1':'01:00','horario2':'02:00'},
    	{'id':1,'nome':'seg','horario1':'07:00','horario2':'08:00'},
    	{'id':1,'nome':'ter','horario1':'09:00','horario2':'10:00'},
    	{'id':1,'nome':'qua','horario1':'01:00','horario2':'02:00'},
		]
    
    //Trabalhando com o filtro através do controller.
    function init(){
    	 var unicos = $filter('unique')($scope.dados, 'horario2');
       console.log(unicos)
    }
    init();
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
 <ul ng-repeat="item in dados| unique:'horario2'">
 <li>{{item.nome}} -- {{item.horario1}} -- {{item.horario2}}</li>
 </ul>
</div>

This way you can work with the filter created directly in Html and also in Controller and so can make the junction you want.

0


Dude, I managed to solve it this way: I create an array where the Dice are the times and I see if this already set, if I’m concateno the day, otherwise I create the position in the array.

var retorno = [];

for(var dado in dados){
   var index = dados[dado].horario1+"-"+dados[dado].horario2;
   if(retorno[index]){
       retorno[index].nome = retorno[index].nome+","+dados[dado].nome;  
   }else{
       retorno[index] = dados[dado];
   }
}
console.log(retorno);
  • Thanks guy, I managed to solve otherwise but not too different, needed a little more lines of code but worked the same way, for my system in case

  • my answer ta up there, but still I will mark yours as the one that solved the problem thanks friend

0

$scope.horariosFormatados = [];
        for (var x = 0; x < $scope.horarios.length; x++) {
            var nome = '';
            var existe = false;
            for (var i = 0; i < $scope.horarios.length; i++) {
                if ($scope.horarios[x].horario_abertura1 === $scope.horarios[i].horario_abertura1 && $scope.horarios[x].horario_abertura2 === $scope.horarios[i].horario_abertura2 && $scope.horarios[x].horario_abertura3 === $scope.horarios[i].horario_abertura3 && $scope.horarios[x].horario_abertura4 === $scope.horarios[i].horario_abertura4) {
                    if (nome === '') {
                        nome = $scope.horarios[i].horario_dia;
                    } else {
                        nome = nome + ", " + $scope.horarios[i].horario_dia;
                    }
                }
            }
            if ($scope.horariosFormatados.length > 0) {
                for (var i = 0; i < $scope.horariosFormatados.length; i++) {
                    console.log($scope.horarios[x].horario_abertura1 + " " + $scope.horariosFormatados[i].hi1);
                    if ($scope.horarios[x].horario_abertura1 === $scope.horariosFormatados[i].hi1 && $scope.horarios[x].horario_abertura2 === $scope.horariosFormatados[i].hf1 && $scope.horarios[x].horario_abertura3 === $scope.horariosFormatados[i].hi2 && $scope.horarios[x].horario_abertura4 === $scope.horariosFormatados[i].hf2) {
                        console.log(existe);
                        existe = true;
                    }
                }
            }
            if (existe === false) {
                $scope.horariosFormatados.push({'dias': nome, 'hi1': $scope.horarios[x].horario_abertura1, 'hf1': $scope.horarios[x].horario_abertura2, 'hi2': $scope.horarios[x].horario_abertura3, 'hf2': $scope.horarios[x].horario_abertura4});
            }
            console.log($scope.horariosFormatados);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.