How to get removed elements of a given array until the passed function returns true in JavaScript ?
Last Updated :
24 May, 2024
The arrays in JavaScript have many methods which make many operations easy.
In this article let us see different ways how to get the removed elements before the passed function returns something. Let us take a sorted array and the task is to remove all the elements less than the limiter value passed to the function, we need to print all the removed elements.
These are the following ways by which we get the removed elements of a given array:
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.
Example:
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);
In a function, if there are multiple return statements only the first return statement gets executed and the function gets completed.
Example: In this example, we will be 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: In this example, we will 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
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.
Example:
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.
Example:
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
Similar Reads
How to remove specific elements from the left of a given array of elements using JavaScript ? In this article, we will learn How to remove specific elements from the left of a given array of elements using JavaScript. We have given an array of elements, and we have to remove specific elements from the left of a given array. Here are some common approaches: Table of Content Using splice() met
2 min read
Which built-in method removes the last element from an array and returns that element in JavaScript ? In this article, we will know how to remove the last element from an array using the built-in method in Javascript, along with understanding its implementation through the examples. There are a few Javascript built-in methods and techniques with which we can remove the last element of the array. We
3 min read
How to Remove Multiple Elements from Array in JavaScript? Here are various methods to remove multiple elements from an array in JavaScript1. Using filter() MethodThe filter() method creates a new array with the elements that pass the condition. This method does not change the original array.JavaScriptlet a = [1, 2, 3, 4, 5]; let remove = [2, 4]; a = a.filt
3 min read
How to Remove First and Last Element from Array using JavaScript? Removing the first and last elements from an array is a common operation. This can be useful in various scenarios, such as data processing, filtering, or manipulation. Example:Input: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]Output: [ 2, 3, 4, 5, 6, 7, 8, 9 ]Removing first and last element in array can be do
2 min read
How to filter values from an array for which the comparator function does not return true in JavaScript ? The task is to filter the array based on the returned value when passed to the given function. The purpose is to loop through the array and execute one function that will return true or false. Then filter out all the values for which the function (comparator function) does not return true. These are
3 min read