How can I union arrays inside an array using lodash?
For example:
Input:
var x = [ [1,2,3,4], [5,6,7], [], [8,9], [] ];
Expected output:
x = [1,2,3,4,5,6,7,8,9];
Currently my code does the following:
return promise.map(someObjects, function (object)) {
return anArrayOfElements();
}).then(function (arrayOfArrayElements) {
// I tried to use union but it can apply only on two arrays
_.union(arrayOfArrayElements);
});
Use apply
method to pass array values as arguments:
var union = _.union.apply(null, arrayOfArrayElements);
[ https://jsfiddle.net/qe5n89dh/ ]
The simplest solution I can think of is to just use concat
:
Array.prototype.concat.apply([], [ [1,2,3,4], [5,6,7],[], [8,9], []]);
Will produce...
[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
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