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

188
Visualizações
Javascript filter function is returning the removed element and not the new array

I am simply trying to remove an element from an array in javascript using the filter function. However, after running this function, newArray is an array with only 1 element - the one I removed.

I wanted an array with the original elements but minus the one I want to remove. How do I get that?

var newArray = comments.filter((comment) => comment.idComment == action.idSectionComments);

Before: enter image description here

After: enter image description here

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You are not removing the element, you are picking it. The filter function return all elements in array that satisfy the condition, so you have to reverse the condition to filter all elements that doesn't match the id

var newArray = comments.filter((comment) => comment.idComment !== action.idSectionComments);
about 4 years ago · Juan Pablo Isaza Relatório

0

var newArray = comments.filter((comment) => comment.idComment !== action.idSectionComments);
about 4 years ago · Juan Pablo Isaza Relatório

0

Maybe reverse:

var newArray = comments.filter((comment) => comment.idComment !== action.idSectionComments);
about 4 years ago · Juan Pablo Isaza 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