Saturday, October 18, 2025
HomeLanguagesJavascriptUnderscore.js _.iterators.reject() Method

Underscore.js _.iterators.reject() Method

With the help of _.iterators.reject() method, we can get the values from iteration function whenever we got false from unary predicate function when we invoked the iterator by using this method.

Syntax: 
 

_.iterators.reject(iter, unaryPredicateFn)

Parameter: This method accepts two parameter as mentioned above and described below: 
 

  • iter: This parameter holds the iterator list of the array.
  • unaryPredicateFn: This parameter holds the unaryPredicateFn function key.

 

Return value: Return the values from iteration function.

 

Note: To execute the below examples, you have to install the underscore-contrib library by using this command prompt we have to execute the following command.

 

npm install underscore-contrib

Below examples illustrate the Underscore.js _.iterators.reject() method in JavaScript: 
 

Example 1: In this example, we can see that by using _.iterators.reject() method, we are able to get the values from iteration function whenever we got false from unary predicate function every time it invoked.

 

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib');
 
var iter = _.iterators.List(["ABC", "Geeks", "XYZ",
                             "for", "Geeks"]);
 
function isGFG (val) {
    if(val == "ABC") {
        return true;
    } else if (val == "XYZ") {
        return true;
    } else {
        return false;
    }
}
 
var geek = _.iterators.reject(iter, isGFG);
 
for(var i = 0; i < 5; i++) {
    console.log(geek());
}


Output:

Geeks
for
Geeks

Example 2:

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib');
 
var iter = _.iterators.List([1, 2, 3, 4, 5, 6, 7]);
 
function isEven (val) {
    if(val%2 == 0) {
        return false;
    } else {
        return true;
    }
}
 
var geek = _.iterators.reject(iter, isEven);
 
for(var i = 0; i < 7; i++) {
    console.log(geek());
}


Output :

2
4
6
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!
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