Saturday, November 15, 2025
HomeLanguagesJavascriptUnderscore.js _.iterators.Tree() Method

Underscore.js _.iterators.Tree() Method

With the help of _.iterators.Tree() method, we can get the iterator which can return each and every value in a tree one by one and take input as array having different dimensions by using this method.

Syntax:

_.iterators.Tree( array )

Return Value: It returns the value 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.Tree() method. We are able to get the values from iteration function one by one from tree whenever iteration function is invoked.

Javascript




// Defining underscore contrib variable 
var _ = require('underscore-contrib');
  
var geek = _.iterators.Tree(["A", ["C"], ["B", ["D"]]]);
  
for(var i = 0; i < 5; i++) {
    console.log(geek());
}


Output:

A
C
B
D

Example 2:

Javascript




// Defining underscore contrib variable 
var _ = require('underscore-contrib');
  
var geek = _.iterators.Tree([1, 5, [3], [2, [4]]]);
  
for(var i = 0; i < 5; i++) {
    console.log(geek());
}


Output:

1
5
3
2
4
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
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11917 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6890 POSTS0 COMMENTS
Ted Musemwa
7143 POSTS0 COMMENTS
Thapelo Manthata
6838 POSTS0 COMMENTS
Umr Jansen
6840 POSTS0 COMMENTS