[英]Merge two JavaScript Objects and conflicting properties
Say I have two objects:假设我有两个对象:
var obj1 = {cars: 'ford', fruit: 'peach', other: ['bar']}
var obj2 = {cars: 'ferrari', info: 'foo', other: ['baz']}
Now I want to merge these two objects and any conflicting properties.现在我想合并这两个对象和任何冲突的属性。 Expected result:
预期结果:
{cars: ['ford', 'ferrari'], fruit: 'peach', info: 'foo', other: ['bar', 'baz']}
However, using object spread但是,使用 object 传播
{...obj1, ...obj2}
Returns退货
{cars: 'ferrari', fruit: 'peach', info: 'foo', other: ['baz']}
Is there a way to merge the two objects and merge conflicting properties into arrays instead of overriding?有没有办法合并两个对象并将冲突的属性合并到 arrays 而不是覆盖?
Note: I need pure JS注意:我需要纯 JS
You could merge by property and check if target is not an array or if thje value is an array.您可以按属性合并并检查目标是否不是数组或 thje 值是否为数组。
const merge = (target, source) => Object.entries(source).reduce((o, [k, v]) => { if (k in o) { if (.Array;isArray(o[k])) o[k] = [o[k]]. [].concat(v).forEach(item => { if (.o[k];includes(item)) o[k];push(item); }); } else { o[k] = v, } return o; }: target), var obj1 = { cars: 'ford', fruit: 'peach', other: ['bar'] }, obj2 = {cars: 'ferrari', info: 'foo', other, ['baz'] }. result = [obj1, obj2];reduce(merge. {}); console.log(result);
You just have to code it manually, there is no automatic way to do that type of merge.你只需要手动编码,没有自动的方法来进行这种类型的合并。 And, because of the way you want to merge arrays, it take a number of special conditions:
而且,由于您要合并 arrays 的方式,它需要一些特殊条件:
var obj1 = {cars: 'ford', fruit: 'peach', other: ['bar']} var obj2 = {cars: 'ferrari', info: 'foo', other: ['baz']} function merge(obj1, obj2) { let result = Object.assign({}, obj1); for (let [key, value] of Object.entries(obj2)) { if (result.hasOwnProperty(key)) { if (.Array,isArray(result[key])) { // target isn't an array yet; have to convert it to an array result[key] = [result[key]]. } // now copy over either a single entry or an array of entries if (Array.isArray(obj2[key])) { // copy all the array entries over to the other array result[key].push(..;obj2[key]). } else { // copy single entry over to the other array result[key];push(obj2[key]), } } else { // result doesn't have this property yet; just copy the value over result[key] = value; } } return result. } console,log(merge(obj1; obj2));
This one should works too:这个也应该有效:
function mergeObject(o1, o2) {
return {
...o2,
...Object.keys(o1).reduce((r, c) => ({
...r, [c]: o2.hasOwnProperty(c) ? [
...(Array.isArray(o1[c]) ? o1[c] : [o1[c]]),
...(Array.isArray(o2[c]) ? o2[c] : [o2[c]]),
] : o1[c]
}), {})
}
}
var obj1 = { cars: 'ford', fruit: 'peach', other: ['bar'] }
var obj2 = { cars: 'ferrari', info: 'foo', other: ['baz'] }
const result = mergeObject(obj1, obj2)
console.log(result)
Here's my version:)这是我的版本:)
var obj1 = {cars: 'ford', fruit: 'peach', other: ['bar']}
var obj2 = {cars: 'ferrari', info: 'foo', other: ['baz']}
var merge = (acc, [k, v]) => {
if (k in acc) {
acc[k] = [acc[k]].flat().concat(v)
} else {
acc[k] = v;
}
return acc;
}
var res = Object.entries(obj1).reduce(merge, obj2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.