Thursday, September 4, 2025
HomeLanguagesJavascriptUnderscore.js _.partition Function

Underscore.js _.partition Function

The _.partition() function is used to get an array as input and returns two arrays. The first array containing those elements that satisfy the predicate (condition) and the second array contains the remaining elements.

Syntax:

_.partition(list, predicate)

Parameters: This function accepts two parameters as mentioned above and described below:

  • list: This parameter holds the list of items.
  • predicate: This parameter holds the truth condition.

Return Value: This function returns two separated array based on predicate condition.

Example 1:




<!DOCTYPE html>
<html>
  
<head>
    <script type="text/javascript" src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
        (function () {
            var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  
            var division = _.partition(arr, function (element) {
                return element % 2 != 0;
            });
  
            console.log(division);
        }());
    </script>
</body>
  
</html>


Output:

Example 2:




<!DOCTYPE html>
<html>
  
<head>
    <script type="text/javascript" src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
        (function () {
            var words = ["javascript", "java", "unix",
                         "hypertext", "underscore", "CSS"];
  
            var part = _.partition(words, function (element) {
                return element.length > 4;
            });
  
            console.log(part);
        }());
    </script>
</body>
  
</html>


Output:

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
32263 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6627 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11858 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS