Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
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 answers
Answer question

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 Report

0

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

0

Maybe reverse:

var newArray = comments.filter((comment) => comment.idComment !== action.idSectionComments);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!