我有两个对象,如果两个道具具有相同的键,我需要合并它们并连接字符串
I have two objects and i need to merge them and concat strings if two props have the same key
我有两个对象,如果两个道具具有相同的键,我需要合并它们并连接字符串
注意:我正在使用 lodash.js
obj1 = {
name: 'john',
address: 'cairo'
}
obj2 = {
num : '1',
address: 'egypt'
}
我需要合并后的对象:
merged = {name:"john", num: "1", address:"cairo,Egypt"}
我尝试使用时地址道具的问题
_.defaults(obj1,obj2)
要么
_.merge(obj1,obj2)
address 值将是 obj2 中 address prop 的值
所以我需要一种方法来合并这两个对象并在每个对象中连接两个地址道具。
试试这个:
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))
您甚至不需要 lodash
。您可以轻松地为此创建自己的功能。你可以这样做:
new Set
- 在两个输入对象 中查找所有唯一的键
for...of
- 遍历上面计算的唯一键
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);
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]));
这样你可以合并任意数量的对象!
您可以使用 _.mergeWith()
将特定键的值连接到数组,然后使用 _.mapValues()
将数组连接到字符串:
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>
我有两个对象,如果两个道具具有相同的键,我需要合并它们并连接字符串 注意:我正在使用 lodash.js
obj1 = {
name: 'john',
address: 'cairo'
}
obj2 = {
num : '1',
address: 'egypt'
}
我需要合并后的对象:
merged = {name:"john", num: "1", address:"cairo,Egypt"}
我尝试使用时地址道具的问题 _.defaults(obj1,obj2) 要么 _.merge(obj1,obj2)
address 值将是 obj2 中 address prop 的值 所以我需要一种方法来合并这两个对象并在每个对象中连接两个地址道具。
试试这个:
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))
您甚至不需要 lodash
。您可以轻松地为此创建自己的功能。你可以这样做:
new Set
- 在两个输入对象 中查找所有唯一的键
for...of
- 遍历上面计算的唯一键
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);
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]));
这样你可以合并任意数量的对象!
您可以使用 _.mergeWith()
将特定键的值连接到数组,然后使用 _.mapValues()
将数组连接到字符串:
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>