With ES6, you have the capability to perform the following:
const arr = [{1:1, 2:2},{3:3},{4:4}];
_.merge(...arr);
This approach is best utilized when targeting new browser versions that support ES6 or when utilizing a transpiler such as Babel.
If you choose not to utilize ES6, then there is no reason to avoid using .apply
, which is a built-in feature of Javascript. It is unnecessary for Lodash to duplicate this functionality. It is important to remember that libraries should complement rather than replace core language features. Other solutions based on loops like .reduce
and .each
are less efficient compared to both _.merge
and Object.assign
, as they can handle more than two parameters efficiently.