Browse Json array in Angularjs

3

I would like to know how to obtain the parallels of each subject in angular js. So far I can get the name but not its parallel Could You Help Me? I have the following JSON:

 [
  {
    "num": "125",
    "nom_coe": "Matematicas",
    "variacion": " ",
    "creditos": "2",
    "grupo_creditos": "",
    "paralelos": [
      {
        "paralelo": "B",
        "dia": "s/d",
        "aula": "NO ASIGNADA",
        "hora_inicio": "12:00:00",
        "hora_fin": "12:00:00"
      }
    ]
  },
  {
    "num": "855",
    "nom_coe": "Historia",
    "variacion": " ",
    "creditos": "4",
    "grupo_creditos": "Genérica",
    "paralelos": [
      {
        "paralelo": "A",
        "dia": "Martes",
        "aula": "NO ASIGNADA",
        "hora_inicio": "10:00:00",
        "hora_fin": "13:00:00"
      }
    ]
  },
  {
    "num": "255",
    "nom_coe": "Lengua",
    "variacion": " ",
    "creditos": "3",
    "grupo_creditos": "",
    "paralelos": [
      {
        "paralelo": "A",
        "dia": "Jueves",
        "aula": "NO ASIGNADA",
        "hora_inicio": "07:30:00",
        "hora_fin": "10:00:00"
      }
    ]
  }
]

This is my code:

$scope.datosComp=data;
    var length = $scope.datosComp.length;
    for ( i=0; i < length; i++) {  
      alert($scope.datosComp[i].nom_coe);
    };

How do I get the parallels?

    
asked by Dimoreno 26.02.2016 в 00:20
source

3 answers

2

How about

$scope.datosComp = data;
var length = $scope.datosComp.length;
for (i = 0; i < length; i++) {
  alert($scope.datosComp[i].nom_coe);
  for (j = 0 j < $scope.datosComp[i].paralelos.length; j++) {
    alert($scope.datosComp[i].paralelos[j].paralelo); //console.log() es mejor :)
  }
};  
    
answered by 26.02.2016 / 00:31
source
4

Angular has angular.forEach . This method invokes the function that it receives, for each element in the array and passes it as a parameter to the element in question.

angular.forEach($scope.datosComp, function(materia) {
  // aquí pones todo el código que quieras para esta única materia.
  console.log("Materia:", materia.nom_coe);

  angular.forEach(materia.paralelos, function(paralelo) {
    // aquí pones todo el código que quieras para este único paralelo de
    // esta única materia.
    console.log("  - Paralelo:", paralelo.paralelo, paralelo.dia, materia.creditos);

  });

}); 
    
answered by 26.02.2016 в 02:20
1

If I understand your question well, you can try something like the following (for example, if you are going to show it in .html ):

function myCtrl($scope){
$scope.faq = [
  {
    "num": "125",
    "nom_coe": "Matematicas",
    "variacion": " ",
    "creditos": "2",
    "grupo_creditos": "",
    "paralelos": [
      {
        "paralelo": "B",
        "dia": "s/d",
        "aula": "NO ASIGNADA",
        "hora_inicio": "12:00:00",
        "hora_fin": "12:00:00"
      }
    ]
  },
  {
    "num": "855",
    "nom_coe": "Historia",
    "variacion": " ",
    "creditos": "4",
    "grupo_creditos": "Genérica",
    "paralelos": [
      {
        "paralelo": "A",
        "dia": "Martes",
        "aula": "NO ASIGNADA",
        "hora_inicio": "10:00:00",
        "hora_fin": "13:00:00"
      }
    ]
  },
  {
    "num": "255",
    "nom_coe": "Lengua",
    "variacion": " ",
    "creditos": "3",
    "grupo_creditos": "",
    "paralelos": [
      {
        "paralelo": "A",
        "dia": "Jueves",
        "aula": "NO ASIGNADA",
        "hora_inicio": "07:30:00",
        "hora_fin": "10:00:00"
      }
    ]
  }
];
}
<div data-ng-app data-ng-controller="myCtrl">
    <div ng-repeat="f in faq">
      <div ng-repeat="f1 in f.paralelos">
             {{f1.paralelo}}
      </div>
    </div>
</div>

link

If I wanted the start time for example, I would just have to use something like this:

{{ f1.hora_inicio }}

Adjusting that you use .nom_coe could be something like this:

<div data-ng-app data-ng-controller="myCtrl">
    <div ng-repeat="f in faq">
             {{ f.nom_coe }}
      <div ng-repeat="f1 in f.paralelos">
             {{ f1.paralelo }}
      </div>
    </div>
</div>

link

    
answered by 26.02.2016 в 09:35