Remove Elements From Array Until Callback Returns True in JavaScript
Last Updated :
11 Dec, 2025
In JavaScript, you can remove elements from an array until a callback function returns true, allowing you to extract only the portion of the array that meets your condition.
- Removes items from the start of the array.
- Stops as soon as the callback returns true.
- Useful for filtering based on dynamic conditions.
- Helps clean and reshape arrays efficiently.
These are the following ways by which we get the removed elements of a given array:
Using for() Loop
In this method, we will take the removed element using the for loop. We can apply the comparator function and get the removed value till the condition gets true.
JavaScript
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let removeArr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] < 6) {
removeArr.push(arr.shift());
} else {
break;
}
}
console.log(removeArr);
Using slice() Method
In a function, if there are multiple return statements only the first return statement gets executed and the function gets completed.
Example: We are using the slice() method to get removed elements of a given array until the passed function returns true in JavaScript
JavaScript
let array = [1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8];
// Removing elements less than 5 and returning them
let limiter = 5;
// function which slices the array taking limiter as parameter
let retrieveRemoved = function (array, limiter) {
let i;
for (i = 0; i < array.length; i++) {
// If the number value is greater or equal than limiter
if (array[i] >= limiter) {
// It takes the array from 0th
// index to i, excluding it
return array.slice(0, i);
}
}
return array.slice(i);
}
let removed = retrieveRemoved(array, limiter);
console.log("The removed elements: " + removed);
OutputThe removed elements: 1,2,2,3,4
Using another array
Another array can be used to check the condition. If it does not satisfy the condition, these are the elements to be removed. We push all the elements that don't satisfy the condition into another array and return the resultant array.
Example: We are be using another array to get removed elements of a given array until the passed function returns true in JavaScript.
JavaScript
let array = [1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8];
// Removing elements less than 5 and returning them
let limiter = 5;
let retrieveRemoved = function (array, limiter) {
let i, s;
let res = [];
for (i = 0; i < array.length; i++) {
if (array[i] < limiter) {
// Push() method is used to
// values into the res[].
res.push(array[i]);
}
else {
s = i;
break;
}
}
return res;
return array.slice(i);
}
let removed = retrieveRemoved(array, limiter);
console.log("The removed elements are: " + removed);
OutputThe removed elements are: 1,2,2,3,4
Using array.filter() Method
The JavaScript Array filter() Method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument method.
JavaScript
let array = [1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8];
let limiter = 5;
function retrieveRemoved(ele) {
return ele < limiter;
}
let removed =
array.filter(retrieveRemoved);
console.log("The removed elements are: " + removed);
OutputThe removed elements are: 1,2,2,3,4
Using Array.splice()
In this approach, we utilize the splice() method to remove elements from the array based on the condition until the passed function returns true. The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
JavaScript
let array = [1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8];
let limiter = 5;
function retrieveRemoved(array, limiter) {
let removed = [];
for (let i = 0; i < array.length; i++) {
if (array[i] < limiter) {
// Splice out the element at index i
// and push it to the removed array
removed.push(array.splice(i, 1)[0]);
i--; // Decrement i as splice shifts the indexes
} else {
break;
}
}
return removed;
}
let removed = retrieveRemoved(array, limiter);
console.log("The removed elements are: " + removed);
console.log("The modified array is: " + array);
OutputThe removed elements are: 1,2,2,3,4
The modified array is: 5,6,6,7,8,8,8
Explore
JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics