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: