다음과 같은 객체가 하나 있습니다 -
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' ] ] }