Underscore.js _.iterators.foldl() Method
Last Updated :
11 Apr, 2023
With the help of _.iterators.foldl() method, we can get the single from the iterator when we called it because it will convert all the values into one by boiling down the given values by using this method.
Syntax:
_.iterators.foldl(iter, binaryFn)
Parameter: This method accepts two parameters as mentioned above and described below:
- iter: This parameter holds the iterator list of the array.
- binaryFn: This parameter holds the binary function key.
Return value: Return the single value when we called an iterator.
Note: To execute the below examples, you have to install the underscore-contrib library by using the command prompt we have to execute the following command.
npm install underscore-contrib
Below example illustrate the Underscore.js _.iterators.foldl() method in JavaScript:
Example 1: In this example, we can see that by using _.iterators.foldl() method, we are able to get the single value returned when we call the iterator because it will boil down the given values into one.
Javascript
const _ = require( 'underscore-contrib' );
const iter = _.iterators.List([ "Geeks" , "for" , "Geeks" ]);
function commaString(a, b) { return a + ", " + b; }
const geek = _.iterators.foldl(iter, commaString);
geek
|
Output:
'Geeks, for, Geeks'
Example 2:
Javascript
const _ = require( 'underscore-contrib' );
const iter = _.iterators.List([ "A" , "ABA" , "ABCBA" , "ABA" , "A" ]);
function spaceString(a, b) { return a + " " + b; }
const geek = _.iterators.foldl(iter, spaceString);
geek
|
Output:
'A ABA ABCBA ABA A'
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...