Angular / Javascript - Re-arrange positions in Array

0
let meuArray = [
   {codigo: 1, nome: Jose,   idade: 33}
   ,{codigo: 2,  nome: Jose,     idade: 32}
   ,{codigo: 3,  nome: Maria,    idade: 31}
   ,{codigo: 4,  nome: Joao,     idade: 30}
   ,{codigo: 5,  nome: Aroldo,   idade: 29}
   ,{codigo: 6,  nome: Aristide, idade: 28}
   ,{codigo: 7,  nome: Aline,    idade: 27}
   ,{codigo: 8,  nome: Amelia,   idade: 26}
   ,{codigo: 9,  nome: Milena,   idade: 25}
   ,{codigo: 10, nome: Andrea,   idade: 24}
]

I have this array of objects (Obvious that in a real example, they are at least 10,000 records). I would reorganize not the rows of the array, but the attributes of the objects. Type: Place Age as First, Code as last. Ex: {age: 24, name: Andrea, code: 10}.

Any ideas? of traditional forms (using for), gets much, much slower.

    
asked by anonymous 14.11.2017 / 15:24

1 answer

0
var texto = [{'codigo': 1, 'nome':'maria', 'idade':20},
             {'codigo': 2, 'nome':'joao', 'idade':20}];
var pessoas = this.texto.map(function (key){
    return {idade: key.idade, nome: key.nome, codigo: key.codigo };
    });

Remembering that you do not need to rearrange the attributes of the object to play in the table or anywhere you want with ng-for, "ngFor", or any other form of bind in AngularJS and Angular;

*. component.html

 <div id="tabela">
    <h2>Bind Com Angular</h2>
    <table>
        <tr>
             <th>Idade</th>
             <th>Nome</th>
             <th>codigo</th>
        </tr>
        <tr *ngFor="let txt of texto" >
          <td>{{txt.idade}}</td>
          <td>{{txt.nome}}</td>
          <td>{{txt.codigo}}</td>    
        </tr>             
    </table>    
</div>

Now, in Angular bind is done in this way, there are others. If you have 10,000 Json records, you will create the 10,000 table.

    
14.11.2017 / 18:46