I just discovered lodash.js and it's freaking GREAT man, however I stumbled onto an example which had me scratching my head.
let numbers = [4, 8, 15, 16, 23, 42];
let isEven = function(n) {
return n % 2 === 0;
};
let [evens, odds] = _.partition(numbers, isEven);
// evens: [4, 8, 16, 42]
// odds: [15, 23]
According to the documentation partition creates ONE array which contains two arrays containing results which returned true or false respectively, then how the outcome of the function TWO arrays?
Because the destructuring assignment allows you to assign the first element of the returned array to variable evens, and the second to odds. Since the returned array contains two arrays, both of the variables now refer to one-dimensional arrays.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With