Remove items from a JavaScript array satisfying a predicate
This post will discuss how to remove elements from an array that satisfy a predicate in JavaScript.
A predicate is a function that returns a boolean value based on some condition. Here are some examples of how to remove elements from an array that satisfy a given predicate in JavaScript given an array and a predicate:
1. Using filter() function
The filter() function allows us to create a new array with all elements that pass a test implemented by a provided function. We can use it to filter out the elements that do not satisfy the predicate from the original array by returning true for all elements that satisfy the predicate. For example, if we have an array and we want to remove all the elements that are not divisible by 2, we can do:
|
1 2 3 4 5 6 7 8 9 10 11 |
var arr = [1, 2, 3, 4, 5]; var predicate = function(item) { // return true for all elements that are divisible by 2 return item % 2 === 0; }; // create a new array with only the elements that satisfy the predicate var newArr = arr.filter(predicate); console.log(newArr); // [2, 4] |
The newer features of JavaScript allow us to write concise functions with a shorter syntax. We can use them to create a copy of the original array without the elements that do not satisfy the predicate by using an arrow function (=>) as the callback for the filter() function. For example, if we have an array [1, 2, 3, 4, 5] and we want to remove all the elements that are less than 3, we can do:
|
1 2 3 4 5 6 7 8 9 |
var arr = [1, 2, 3, 4, 5]; // return true for all elements that are greater than or equal to 3 var predicate = item => (item >= 3); // create a copy of the original array without the elements that do not satisfy the predicate var newArr = arr.filter(predicate); console.log(newArr); // [3, 4, 5] |
2. Using a for loop
This is another technique that works in any version of JavaScript. It involves using a for loop to iterate over the array from the end to the beginning, and using the splice() function to remove the element at the current index if it does not satisfy the predicate. To remove elements by the splice() function, we can specify the start index and the number of elements to delete. For example, if we have an array and we want to remove all the elements that are odd, we can do:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var arr = [1, 2, 3, 4, 5]; var predicate = function(item) { // return true for all elements that are odd return item % 2 !== 0; }; // loop backwards to avoid index shifting for (var i = arr.length - 1; i >= 0; i--) { // check if the element does not satisfy the predicate if (!predicate(arr[i])) { // remove one element at the current index arr.splice(i, 1); } } console.log(arr); // [1, 3, 5] |
That’s all about removing elements from an array satisfying a given predicate in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)