Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
Adding and removing an element in an array

There are 2 toggle buttons. If the value is true, then add to the array, otherwise remove the element.

data:

originality: []

toggles:

<toggle id='1' ref='toggleOriginal'> Click </toggle>
<toggle id='2' ref='toggleAnalog'> Click </toggle>

methods:

 if(this.$refs.toggleOriginal.isActive) {
    this.originality.push(this.$refs.toggleOriginal.id);
 } else {
    this.originality = this.originality.filter((item) => {
      return item == this.$refs.toggleOriginal.id;
   });
 }

 if(this.$refs.toggleAnalog.isActive) {
    this.originality.push(this.$refs.toggleAnalog.id);
  } else {
    this.originality = this.originality.filter((item) => {
      return item == this.$refs.toggleAnalog.id;
    });
  }

And the same for the second. In isActive I check for true / false. the problem is that if two toggle is true and I want to convert one to false, then the wrong element is removed. Perhaps you should use a different functionality?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

To remove from array using filter, you should test for non-equality. Your example uses equality, and that will remove all but your item.

this.originality = this.originality.filter((item) => {
      return item !== this.$refs.toggleOriginal.id;
   });

or splice

const index = this.originality.indexOf(this.$refs.toggleOriginal.id)
this.originality.splice(index, 1)
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda