Open In App

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:

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:


Article Tags :