Doubt about the map function

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"
            }
        },]
}

I need to turn it into something like this

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

]

I tried to do this, but it did not work, does anyone know how to do it?

this.names = this.students.map(function(student){
    console.log(student.aluno.nome);
      return student.aluno.nome;
  })
    
asked by anonymous 28.01.2018 / 20:37

1 answer

3

I do not know if it really is necessary to create another layout of this information just to print, but, an example of how it would be 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><divng-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><divng-app="app" ng-controller="ctrl">
  <ul>
    <li ng-repeat="item in alunos.alunos">{{item.aluno.nome}} - {{item.mensalidade.status}}</li>
  </ul>
</div>
    
28.01.2018 / 21:12