Skip to content
Related Articles
Open in App
Not now

Related Articles

How to filter values from an array for which the comparator function does not return true in JavaScript ?

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 09 Dec, 2022
Improve Article
Save Article

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.

Approach: If the given array is [5, 6, 7, 8, 9, 2, 6, 3, -4, 0, -9, -6] and the task is to filter out the negative values and want to print that values.

Let us create a function that returns true if the value is positive otherwise it returns false.

JavaScript Pseudo Code:

Javascript




<script>
// Comparator function
const myFilter = (element) => {
  if(element >= 0){
      return true;
  }
  else{
      return false;
  }
</script>


In order to filter out the array, we will loop through the array and call this function “myFilter“. The value is skipped if it returns true, if the function returns false, then we will store that value in the filteredArr by using “filteredArr.push()” in the filtered array.  We use forEach through the array of elements.

Example 1: In this example, we will filter the negative values from the array using the forEach() method.

Javascript




<script>
    const arr = [5, 6, 7, 8, 9, 2, 6, 3, -4, 0, -9, -6];
      
    // Filtered array for which function
    // returned false
    var filteredArr = [];
      
    // Comparator function
    const myFilter = (element) => {
        if (element >= 0) {
            return true;
        }
        else {
            return false;
        }
    }
      
    // Iterate through each element 
    arr.forEach(element => {
      
        // Push in the filteredArr if 
        // it returns false
        if (myFilter(element) === false) {
            filteredArr.push(element);
        }
    })
      
    console.log("After filtering:", filteredArr);
</script>


Output:

After filtering : [ -4, -9, -6 ]

Example 2: To filter the positive values, we can either change our myFilter function or the code section of the forEach loop. If we change the condition in the myFilter function for less than zero, the function will return false for a positive value instead. We will store those positive values in filteredArr.

Javascript




<script>
    // Data to filter
    const arr = [5, 6, 7, 8, 9, 2, 6, 3, -4, 0, -9, -6];
      
    // Filtered array for which function
    // returned false
    var filteredArr = [];
      
    // Comparator function
    const myFilter = (element) => {
        if (element < 0) {
            return true;
        }
        else {
            return false;
        }
    }
      
    // Iterate through each element 
    arr.forEach(element => {
      
        // Push in the filteredArr if
        // function return false
        if (myFilter(element) === false) {
            filteredArr.push(element);
        }
    })
      
    console.log("After filtering :", filteredArr);
</script>


Output:

After filtering : [ 5, 6, 7, 8, 9, 2, 6, 3, 0 ]

We have successfully filtered the array for which the function does not return true.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!