Question about the map function

Asked

Viewed 42 times

1

I have this object

    {
    "alunos": [
        {
            "aluno": {
                "id": 1,
                "nome": "Genevieve Sipes",
                "status": "Ativo"
            },
            "mensalidade": {
                "status": "Débito"
            }
        },
        {
            "aluno": {
                "id": 2,
                "nome": "Greyson Herman",
                "status": "Ativo"
            },
            "mensalidade": {
                "status": "Débito"
            }
        },
        {
            "aluno": {
                "id": 3,
                "nome": "Yessenia Emmerich",
                "status": "Ativo"
            },
            "mensalidade": {
                "status": "Débito"
            }
        },]
}

And I need to turn him into something like this

[
   {
      nome: 'Genevieve Sipes',
      status: 'Débito',
   },

]

I tried to do it this way, but it didn’t work, someone knows how to do it ?

this.names = this.students.map(function(student){
    console.log(student.aluno.nome);
      return student.aluno.nome;
  })

1 answer

3

I don’t know, if it really is necessary to create another layout of this information just to print, but, an example of how would be a way to apply this new data layout:

var app = angular.module('app', []);
app.controller('ctrl', function($scope) {
  $scope.alunos = {
    "alunos": [{
        "aluno": {
          "id": 1,
          "nome": "Genevieve Sipes",
          "status": "Ativo"
        },
        "mensalidade": {
          "status": "Débito"
        }
      },
      {
        "aluno": {
          "id": 2,
          "nome": "Greyson Herman",
          "status": "Ativo"
        },
        "mensalidade": {
          "status": "Débito"
        }
      },
      {
        "aluno": {
          "id": 3,
          "nome": "Yessenia Emmerich",
          "status": "Ativo"
        },
        "mensalidade": {
          "status": "Débito"
        }
      },
    ]
  };
  $scope.names = $scope.alunos.alunos.map(function(student)
  {    
      return {nome:student.aluno.nome, status:student.mensalidade.status};
  });      
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="ctrl">
  <ul>
    <li ng-repeat="item in names">{{item.nome}} - {{item.status}}</li>
  </ul>
</div>

In the original form of the layout you can also print the information in the same way:

var app = angular.module('app', []);
app.controller('ctrl', function($scope) {
  $scope.alunos = {
    "alunos": [{
        "aluno": {
          "id": 1,
          "nome": "Genevieve Sipes",
          "status": "Ativo"
        },
        "mensalidade": {
          "status": "Débito"
        }
      },
      {
        "aluno": {
          "id": 2,
          "nome": "Greyson Herman",
          "status": "Ativo"
        },
        "mensalidade": {
          "status": "Débito"
        }
      },
      {
        "aluno": {
          "id": 3,
          "nome": "Yessenia Emmerich",
          "status": "Ativo"
        },
        "mensalidade": {
          "status": "Débito"
        }
      },
    ]
  }; 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="ctrl">
  <ul>
    <li ng-repeat="item in alunos.alunos">{{item.aluno.nome}} - {{item.mensalidade.status}}</li>
  </ul>
</div>

  • It is that I am using the semantic autocomplete and at the time of passing the data it $('.ui.search') . search({ source : content, searchFields : [ 'name ], searchFullText: false })

  • @Mateushenrique, I understand, the solution was useful to you, solved your problem?

Browser other questions tagged

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