merge
The mergeObjects()
function merges two or more objects into a new object.
Parameters
Parameter | Type | Description |
---|---|---|
objects | Object | The objects to merge. |
Returns
Object
: The merged object.
Examples
Merging two objects
const utils = require('utils-core.js');
const object1 = {
name: "sif",
age: 30,
hobbies: ["reading", "running"]
};
const object2 = {
address: {
street: "123 Main St",
city: "New York",
state: "NY"
},
contact: {
email: "sif@sifedine.lol",
phone: "555-1234"
}
};
const mergedObj = utils.objects.mergeObjects(object1, object2);
console.log(mergedObj);
// Output: {
// name: "sif",
// age: 30,
// hobbies: ["reading", "running"],
// address: {
// street: "123 Main St",
// city: "New York",
// state: "NY"
// },
// contact: {
// email: "sif@sifedine.lol",
// phone: "555-1234"
// }
// }
Merging three objects
const utils = require('utils-core.js');
const object1 = {
name: "sif",
age: 30,
hobbies: ["reading", "running"]
};
const object2 = {
address: {
street: "123 Main St",
city: "New York",
state: "NY"
}
};
const object3 = {
contact: {
email: "sif@sifedine.lol",
phone: "555-1234"
}
};
const mergedObj = utils.objects.mergeObjects(object1, object2, object3);
console.log(mergedObj);
// Output: {
// name: "sif",
// age: 30,
// hobbies: ["reading", "running"],
// address: {
// street: "123 Main St",
// city: "New York",
// state: "NY"
// },
// contact: {
// email: "sif@sifedine.lol",
// phone: "555-1234"
// }
// }
info
when merging objects with overlapping keys (keys that exists in two or more objects), the last objects with the key will overrides the ones before it.
For example merging two objects that has age key:
const utils = require('utils-core.js');
const object1 = {
name: "sif",
age: 30,
hobbies: ["reading", "running"]
};
const object2 = {
age: 35,
address: {
street: "123 Main St",
city: "New York",
state: "NY"
},
contact: {
email: "sif@sifedine.lol",
phone: "555-1234"
}
};
const mergedObj = utils.objects.mergeObjects(object1, object2);
console.log(mergedObj);
// Output: {
// name: "sif",
// age: 35, // value from object2 overwrites value from object1
// hobbies: ["reading", "running"],
// address: {
// street: "123 Main St",
// city: "New York",
// state: "NY"
// },
// contact: {
// email: "sif@sifedine.lol",
// phone: "555-1234"
// }
// }
In this example we have:
- The age value from
object2
overwrites value fromobject1
Merging three objects with overlapping keys:
const utils = require('utils-core.js');
const object1 = {
name: "Alice",
age: 30,
hobbies: ["reading", "yoga"],
address: {
street: "123 Main St",
city: "New York",
state: "NY"
}
};
const object2 = {
age: 35,
hobbies: ["running", "swimming"],
address: {
street: "456 Elm St",
city: "Boston",
state: "MA",
zip: "02115"
}
};
const object3 = {
name: "Bob",
hobbies: ["painting"],
address: {
city: "San Francisco"
}
};
const mergedObj = utils.objects.mergeObjects(object1, object2, object3);
console.log(mergedObj);
// Output: {
// name: "Bob",
// age: 35, //
// hobbies: ["painting"],
// address: {
// street: "456 Elm St",
// city: "San Francisco",
// state: "MA",
// zip: "02115"
// }
// }
In this example we have:
- The name value from
object3
overwrites value fromobject1
- The age value from
object2
overwrites value fromobject1
- The hobbies value from
object3
overwrites value fromobject2
, which overwrote value fromobject1
- The street value from
object2
overwrites value fromobject1
- The city value from
object2
overwrites value fromobject1
- The city value from
object3
overwrites value fromobject1
- The state value from
object2
overwrites value fromobject1
- The zip value from
object2