{
const arrBig = [
{ _id: 1, one: 7, two: 5 },
{ _id: 2, one: 6, two: 3 },
{ _id: 3, one: 4, two: 7 },
{ _id: 4, one: 15, two: 2 },
];
const arrSmall = [{ _id: 1, next: 22, gen: 54, asd: 'asd' }, { _id: 3, next: 6, gen: 3 }];
const keyId = '_id';
const keysToCopy = ['next', 'gen'];
const pick = (obj, keys) => keys.reduce((acc, key) => {
acc[key] = obj[key];
return acc;
}, {});
const template = Object.fromEntries(keysToCopy.map(key => [key, 0]));
const dictSmall = arrSmall.reduce((acc, c) => {
acc[c[keyId]] = pick(c, keysToCopy);
return acc;
}, {});
const result = arrBig.map(item => ({ ...item, ...template, ...dictSmall[item[keyId]] }));
console.table(result)
}
const dictSmall = arrSmall.reduce((acc, c) => {
acc[c[key]] = c;
return acc;
}, {});
const result = arrBig.map(item => { const nuItem = { ...item, ...template }; const addendum = dictSmall[item[key]]; if (addendum) { addKeys.forEach(key => (nuItem[key] = addendum[key])); } return nuItem; });
const result = arrBig.map(item => ({ ...item, ...template, ...dictSmall[item[key]] }));