Add object inside an object

Asked

Viewed 2,001 times

1

is the following, I have 3 tables. Cities, Organs and Motives. 1 City can have several organs, and 1 organ several motives.

My problem is that in a single page it has to appear all the organs of the city and inside the organ all the motives of the organ. I’m not able to put the motives inside the organ, matter of logic even.

I’m making an ng-repeat of organs that are referenced to the city

$http.get(base_url + 'controlx/functions/getWhere/orgaos/cidades_id/'
       + $stateParams.cidadeId) // Aqui pega os orgãos referenciado a cidade
.success(function(data) {
  $scope.orgaos = data;
  var i = 0;
  var motivo = [];
  for (let orgao of $scope.orgaos){
    $http.get(base_url + 'controlx/functions/getWhere/motivos/orgaos_id/'+orgao.id) // aqui faz uma repetição referenciado ao orgão.
    .success(function(data) {
      motivo.push(data);
      $scope.orgaos=motivo[i];
      i++;
    });
  }
  console.log(motivo);
  if (data.length === 0) {
    $scope.erro = 'Nenhum Orgão Encontrado';
  }
});

So I was trying to add the motifs inside each organ. But so it’s not working. I wanted to basically put the motifs inside each respective organ ($Scope.orgaos). As if to give a . push, but it doesn’t work on objects.

To understand how I want: Essa foto do sistema.

  • Good morning @Viniusvilela, seeing your code, I can already get a clue as to what might be going wrong. But can you tell if there is an error in the console? and what the output of console.log(motivo);?

  • It returns arrays (organs) with arrays inside (motifs)

  • and no error on console?

  • No, no error on console

  • I wanted to put the motifs inside each respective organ ($Scope.orgaos) as if to give a . push, but it doesn’t work on objects.

  • What do you get from the two requests are arrays? What is the structure of this data?

  • [http://imgur.com/ZDbPSLG] This is what returns on the console.log(reason). The first organ has 2 motifs and the second

  • What I get from requests are Objects.

Show 3 more comments

2 answers

2


Well, since you didn’t post the data schema, I will assume that you receive an Object Array in both requests. Then you can do the following:

$http.get(base_url + 'controlx/functions/getWhere/orgaos/cidades_id/' + $stateParams.cidadeId) // Aqui pega os orgãos referenciado a cidade
.success(function(data) {

    /* 
        Supondo que aqui você recebe um Array de Objetos.
        Seria algo assim: 
            [
                { id: 'id_orgao_1' },
                { id: 'id_orgao_2' },
                { id: 'id_orgao_3' }
            ]
    */
    $scope.orgaos = data;

    for (let orgao of $scope.orgaos){
        $http.get(base_url + 'controlx/functions/getWhere/motivos/orgaos_id/'+orgao.id) // aqui faz uma repetição referenciado ao orgão.
        .success(function(data) {

            /* 
                Supondo que aqui você recebe um Array.
                Aqui você cria uma nova chave dentro de cada objeto contendo este Array
            */
            orgao.motivos = data

        });
    }

    console.log($scope.orgaos);
    /*

     Sua estrutura final:

            [
              {
                "id": "id_orgao_1",
                "motivos": [ 
                  "motivo1 do orgao1",
                  "motivo2 do orgao1",
                  "motivo3 do orgao1"
                ]
              },
              {
                "id": "id_orgao_2",
                "motivos": [
                  "motivo1 do orgao2",
                  "motivo2 do orgao2",
                  "motivo3 do orgao2"
                ]
              },
              {
                "id": "id_orgao_3",
                "motivos": [
                  "motivo1 do orgao3",
                  "motivo2 do orgao3",
                  "motivo3 do orgao3"
                ]
              }
            ]
    */


    if (data.length === 0) {
        $scope.erro = 'Nenhum Orgão Encontrado';
    }
});
  • Wow, your beautiful, thanks. Gave certificate :3

  • @Viníciusvilela Great! Good luck on the project!

0

Amigo. It would be interesting to do a basic javascript and make the logic work with the objects, then we could contribute easier with the solution of the problem and replicate locally

follows basic script I made, if I can adjust it according to your need we can evaluate the problem :)

  var orgaos = { "cidade" : ["SP", "RJ"] }

  var i = 0;
  var motivo = [];
  for (let orgao of orgaos.cidade){

      motivo.push('teste');
      orgaos=motivo[i];
      i++;

  }
  console.log(motivo);

</script>

  • So, you got it wrong, the city has organs, and the organs have motifs kk

  • my problem is not syntax or anything like that, it’s in logic even if it’s not working.

  • Glad you settled up :)

Browser other questions tagged

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