Related Articles

Related Articles

Underscore.js _.iterators.select() Method
  • Last Updated : 01 Sep, 2020

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

Syntax:

_.iterators.select(iter, unaryPredicateFn)

Parameter: This method accepts two parameter as mentiond 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

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

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

Geeks
for
Geeks

Example 2:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

1
3
5
7

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :