The partition
function from lodash allows you to split an array into two groups based on a given condition or predicate function.
Here's an example of how to use the partition
function:
index.tsx249 chars9 lines
In the above example, we use the partition
function from lodash to split the numbers
array into two groups - even numbers and odd numbers. We pass a predicate function as the second argument to the partition
function. This function returns true
for even numbers, which ends up grouping all even numbers in one array and odd numbers in another array.
The partition
function returns an array of two arrays in this format - [matchingElementsArray, nonMatchingElementsArray]
. In our example, evenNumbers
array contains all even numbers from the numbers
array and oddNumbers
array contains all odd numbers.
gistlibby LogSnag