Object.defineProperty(Object, "assignMerge", {
value: function assign(target, varArgs) {
'use strict';
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) {
for (var nextKey in nextSource) {
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
if(to.hasOwnProperty(nextKey) && nextSource.hasOwnProperty(nextKey) && Array.isArray(to[nextKey]) && Array.isArray(nextSource[nextKey])){
to[nextKey] = to[nextKey].concat(nextSource[nextKey])
}
}
}
}
}
return to;
},
writable: true,
configurable: true
});
const mergedObjects = Object.assignMerge({a:[1, 2], b:[2], c:[2]}, {a:[2,5], b:[1]});
console.log(mergedObjects)
Я думаю, что если вы создаете свой собственный метод и применяете тот же принцип, который вы применяете, который используется для Object.assign
polyfill, он может выглядеть следующим образом.