Open In App
Related Articles

How to get removed elements of a given array until the passed function returns true in JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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:

Method 1: 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.

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);

                    

Output
[ 1, 2, 3 ]


Method 2: 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: 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);

                    

Output
The removed elements: 1,2,2,3,4

Method 3: 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);

                    

Output
The removed elements are: 1,2,2,3,4

Method 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.

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);

                    

Output
The removed elements are: 1,2,2,3,4



Last Updated : 21 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads