Saturday, October 25, 2025
HomeLanguagesJavascriptHow to filter values from an array for which the comparator function...

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 the following methods for filtering the values from an array:

Approach:

  • Start by defining an array named ‘array’ and assign the values to it.
  • Define a comparator function name compare that will be used to filter the elements.
  • Apply different methods to get the filtered values.

Method 1: Using forEach() Method

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: In this example, we will filter the negative values from the array using the forEach() method.

Javascript




const arr = [5, 6, 7, 8, 9, 2, 6, 3, -4, 0, -9, -6];
 
// Filtered array for which function
// returned false
let 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);


Output

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

Method 2: Using filter() Method

In this method, we will use the filter() method and comparator function. The comparator function gives the true for odd numbers because value%2 gives the zero which is false in the boolean.

Example:

Javascript




let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let filteredArr = [];
const myFilter = (value) => {
  return Boolean(value % 2);
};
 
arr.filter((element) => {
  if (myFilter(element) === false) {
    filteredArr.push(element);
  }
});
 
console.log(filteredArr);


Output

[ 2, 4, 6, 8, 10 ]

Method 3: Using for() loop

In this method, we will use for() loop for iterating over the array and then use the comparator function for comparing the values.

Example:

Javascript




const arr = [5, 6, 7, 8, 9, 2, 6, 3, -4, 0, -9, -6];
// Filtered array for which function
// returned false
let filteredArr = [];
 
// Comparator function
const myFilter = (element) => {
    if (element >= 0) {
        return true;
    }
    else {
        return false;
    }
}
for (i = 0; i < arr.length; i++) {
    if (myFilter(arr[i])===false) {
        filteredArr.push(arr[i]);
    }
}
console.log(filteredArr);


Output

[ -4, -9, -6 ]

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS