I have two objects and i need to merge them and concat strings if two props have the same key note: I'm using lodash.js
obj1 = {
name: 'john',
address: 'cairo'
}
obj2 = {
num : '1',
address: 'egypt'
}
I need the merged object to be like:
merged = {name:"john", num: "1", address:"cairo,Egypt"}
the issue with address prop when I'm trying to use _.defaults(obj1,obj2) or _.merge(obj1,obj2)
the address value will be the value of the address prop inside obj2 so I need a way to merge the two objects and concat the two address props in each object.
CodePudding user response:
Try this:
function merge(obj1, obj2) {
let merged = {
...obj1,
...obj2
};
Object.keys(obj1).filter(k => obj2.hasOwnProperty(k)).forEach((k) => {
merged[k] = obj1[k] "," obj2[k]
})
return merged
}
obj1 = {
name: 'john',
address: 'cairo'
}
obj2 = {
num: '1',
address: 'egypt'
}
console.log(merge(obj1, obj2))
CodePudding user response:
You don't even need lodash
. You can easily create you own function for that. You can do it like this:
new Set
- to find all unique keys across both input objectsfor...of
- to iterate over unique keys calculated above
function mergeObjects(obj1, obj2) {
let merged = {};
for (const property of [...new Set([...Object.keys(obj1),...Object.keys(obj2)])]) {
if(obj1[property]) merged[property] = obj1[property];
if(obj2[property]) merged[property] = merged[property] ? `${merged[property]},${obj2[property]}` : obj2[property];
}
return merged;
}
const obj1 = { name: 'john', address: 'cairo' };
const obj2 = { num : '1', address: 'egypt' };
const mergedObj = mergeObjects(obj1, obj2);
console.log(mergedObj);
CodePudding user response:
function mergeObjects(objArr) {
const newObj = {};
objArr.forEach((element) => {
const keys = Object.keys(element);
keys.forEach((key) => {
if (newObj[key]) newObj[key] = newObj[key] "," element[key];
else newObj[key] = element[key];
});
});
return newObj;
}
const obj1 = {name: 'john', address: 'cairo'}
const obj2 = { num : '1', address: 'egypt'}
console.log(mergeObjects([obj1, obj2]));
This way you can merge any number of objects!
CodePudding user response:
You can use _.mergeWith()
to concat the values of specific keys to arrays, and then use _.mapValues()
to join the arrays to strings:
const { mergeWith, mapValues, uniq, join } = _;
const fn = (predicate, ...objs) => mapValues(
// merge the objects
mergeWith(
{},
...objs,
// handle the keys that you want to merge in a uniqe way
(a = [], b = [], key) => predicate(key) ? a.concat(b) : undefined
),
// transform the relevant keys' values to a uniqe strings
(v, key) => predicate(key) ? uniq(v).join(', ') : v
);
const obj1 = { name: 'john', address: 'cairo' };
const obj2 = { num : '1', address: 'egypt' };
const result = fn(key => key === 'address', obj1, obj2);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG ljU96qKRCWh quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>