UNPKG

hd-utils

Version:

A handy utils for modern JS developers

34 lines (33 loc) 1.09 kB
import isObject from '../validation/isObject'; export default function joinObjects2(target, ...sources) { let newObj = isObject(target) ? { ...target } : {}; for (let index = 0; index < sources.length; index++) { const object = sources[index]; if (isObject(object)) { for (const key in object) { if (Object.prototype.hasOwnProperty.call(object, key)) { newObj[key] = object[key]; } } } else { continue; } } return newObj; } // const otraObj = { // a: 1, // }; // function name<T>(object: Partial<T>) { // return joinObjects2(object, {b:1}); // } // // Example usage // const result1 = joinObjects2({ a: 1 }, { b: 2 }); // { a: 1, b: 2 } // const result2 = joinObjects2({ a: 1 }, null, undefined, { c: 3 }); // { a: 1, c: 3 } // const result3 = joinObjects2([1, 2]); // {} // const a = { foo: 1 }; // const b = { bar: 1 }; // const x = { ...a, ...b }; // const y = Object.assign(a, b, { n: 1 }); // console.log({ result1, result2, result3 });