How to use for() Loop In Javascript
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:
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 ]
How to get removed elements of a given array until the passed function returns true in JavaScript ?
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:
Table of Content
- Using for() Loop
- Using slice() Method
- Using another array
- Using array.filter() Method
- Using Array.splice()