Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

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

0

Maybe reverse:

var newArray = comments.filter((comment) => comment.idComment !== action.idSectionComments);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda