ID repeated on objects in my array

0

I'm trying to assign an ID to each object in my array, but when I have two or more like objects, they end up getting the same ID. I already tried to indexOf() and it also did not work.

this.selection.selected.forEach(servico => {
   servico.id = this.contador;
   this.adicionados.push(servico);
   this.contador++;
});

List with different objects - List with 1 repeated object

    
asked by anonymous 17.12.2018 / 14:22

1 answer

0

As I understand it, they are repeated because you are always adding to the same array without first clearing it.

See if the following code solves your problem

const novosAdicionados = [];
this.selection.selected.forEach(servico => {
    servico.id = this.contador;
    novosAdicionados.push(servico);
    this.contador++;
 });

 this.adicionados = novosAdicionados;

If not, you need to give more information / code to help you.

    
20.12.2018 / 13:05