Angular / Javascript - Re-organizing positions in the Array

Asked

Viewed 263 times

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 (Obviously in a real example, they are a minimum of 10,000 records). I would like to reorder not the array lines, but the object attributes. Type: Set Age to First, Code to Last. Ex: {age: 24, name: Andrea, code: 10}.

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

  • Why?????????????????? this has no effect, what is the reason why ?

  • What do you mean? Reorganize the properties? That doesn’t make any sense!

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 };
    });

Remember that you do not need to rearrange the attributes of the object to play on the table or wherever 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>

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

  • More is exactly what I need! I want to change the column positions of a Property bind table! How would I do that?

  • @Luizfelipe I hope I helped!

  • The bind Property I can do! was to rearrange the columns. thanks

Browser other questions tagged

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