我想将第一个对象分配给具有相同属性的第二个对象。您建议的快速方法是什么?我是否需要分别遍历store对象?
第一个对象:
{
title: 'test',
description: 'desc',
tags: ['tag1','tag2']
}
第二个对象:
{
title: '',
description: '',
tags: [],
stores: [
{
id: 1,
title: '',
description: '',
tags: '',
template: '',
link: '',
active: true
},
{
id: 2,
title: '',
description: '',
tags: '',
template: '',
link: '',
active: false
}
]
}
结果:
{
title: 'test',
description: 'desc',
tags: ['tag1', 'tag2'],
stores: [
{
id: 1,
title: 'test',
description: 'desc',
tags: '['tag1', 'tag2']',
template: '',
link: '',
active: true
},
{
id: 2,
title: 'test',
description: 'desc',
tags: '['tag1', 'tag2']',
template: '',
link: '',
active: false
}
]
}
答案 0 :(得分:1)
您可以使用对象传播运算符进行浅层合并。
尝试一下。
let obj1 = {
title: 'test',
description: 'desc',
tags: ['tag1', 'tag2']
};
let obj2 = {
title: '',
description: '',
tags: [],
stores: [
{
id: 1,
title: '',
description: '',
tags: '',
template: '',
link: '',
active: true
},
{
id: 2,
title: '',
description: '',
tags: '',
template: '',
link: '',
active: false
}
]
};
console.log({...obj2, ...obj1});
如果要合并深度嵌套的对象,则可以使用以下方法。
let obj1 = {
title: 'test',
description: 'desc',
tags: ['tag1', 'tag2']
};
let obj2 = {
title: '',
description: '',
tags: [],
stores: [
{
id: 1,
title: '',
description: '',
tags: '',
template: '',
link: '',
active: true
},
{
id: 2,
title: '',
description: '',
tags: '',
template: '',
link: '',
active: false
}
]
};
function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
function deepMerge(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
return deepMerge(target, ...sources);
}
console.log(deepMerge(obj2, obj1));
答案 1 :(得分:1)
您可能希望某种递归函数遍历所有键并检查它们是否存在于另一个对象上,例如:
let a = {
title: "test",
description: "desc",
tags: ["tag1", "tag2"],
};
let b = {
title: "",
description: "",
tags: [],
stores: [{
id: 1,
title: "",
description: "",
tags: "",
template: "",
link: "",
active: true,
},
{
id: 2,
title: "",
description: "",
tags: "",
template: "",
link: "",
active: false,
},
],
};
const merge = (a, b) => {
Object.keys(b).forEach((key) => {
if (typeof b[key] === "object" && key !== 'tags') {
merge(a, b[key]);
}
if (Object.keys(a).includes(key)) {
b[key] = a[key];
}
});
};
merge(a, b);
console.log(b);
console.log(b.stores[0].tags); // [ 'tag1', 'tag2' ]
我必须添加对键tags
的大小写的检查,尽管它是一个对象,但我们不想更深入地研究它。尽管如此,这应该为您指明正确的方向。
答案 2 :(得分:0)
let obj = {
title: 'test',
description: 'desc',
tags: ['tag1','tag2']
};
let obj2 = {
title: '',
description: '',
tags: [],
stores: [
{
id: 1,
title: '',
description: '',
tags: '',
template: '',
link: '',
active: true
},
{
id: 2,
title: '',
description: '',
tags: '',
template: '',
link: '',
active: false
}
]
}
console.log(Object.assign(obj2, obj))
只需使用Object.assign
,并在两个对象之间作为参数进行翻转