两个对象的JavaScript并集
我们有一个这样的物体-
const obj1 = { name: " ", email: " " };和另一个像这样-
const obj2 = { name: ['x'], email: ['y']};我们需要编写一个接受两个这样的对象的JavaScript函数。并希望输出是这样的联合-
const output = { name: {" ", [x]}, email: {" ", [y]} };示例
为此的代码将是-
const obj1 = { name: " ", email: " " };
const obj2 = { name: ['x'], email: ['y']};
const objectUnion = (obj1 = {}, obj2 = {}) => {
const obj3 = {
name:[],
email:[]
};
for(let i in obj1) {
obj3[i].push(obj1[i]);
}
for(let i in obj2) {
obj3[i].push(obj2[i]);
}
return obj3;
};
console.log(objectUnion(obj1, obj2));输出结果
控制台中的输出将是-
{ name: [ ' ', [ 'x' ] ], email: [ ' ', [ 'y' ] ] }热门推荐
10 生日祝福语朋友简短的话
11 宝贝的人生祝福语简短
12 新年有哪些祝福语简短
13 简短又洋气祝福语大全
14 简短祝福语除夕快乐英文
15 生日贺卡祝福语毕业简短
16 战友聚会祝福语幽默简短
17 最新中秋祝福语简短英语
18 夏日升学宴祝福语简短