I want to filter an array of objects based on a specific condition. Specifically, I want to exclude the object with an ID equal to a given value and all objects that appear before the first object that doesn’t satisfy this condition. How can I achieve this using a single loop?
Here’s an example array:
const arr = [{ id: 10 }, { id: 20 }, { id: 30 }, { id: 40 }, { id: 50 }, { id: 60 }];
Let’s say I have a target ID:
const myID = 25;
I want to filter the array to get the following result:
const result = [{ id: 20 }, { id: 30 }, { id: 40 }, { id: 50 }, { id: 60 }];
3
Answers
You can achieve this using a combination of
filter
andfind
array methods. Here’s how you can do it:This will give you the desired result: