다음과 같은 객체 배열이 있다고 가정해 보겠습니다. -
const arr = [{
name: 'Dinesh Lamba',
age: 23,
occupation: 'Web Developer',
}, {
address: 'Vasant Vihar',
experience: 5,
isEmployed: true
}]; 우리는 그러한 객체 배열 중 하나를 취하는 JavaScript 함수를 작성해야 합니다. 그런 다음 함수는 배열의 모든 개체에 있는 모든 속성을 포함하는 개체를 준비해야 합니다.
따라서 위의 배열의 경우 출력은 다음과 같아야 합니다. -
const output = {
name: 'Dinesh Lamba',
age: 23,
occupation: 'Web Developer',
address: 'Vasant Vihar',
experience: 5,
isEmployed: true
}; 예시
다음은 코드입니다 -
const arr = [{
name: 'Dinesh Lamba',
age: 23,
occupation: 'Web Developer',
}, {
address: 'Vasant Vihar',
experience: 5,
isEmployed: true
}];
const mergeObjects = (arr = []) => {
const res = {};
arr.forEach(obj => {
for(key in obj){
res[key] = obj[key];
};
});
return res;
};
console.log(mergeObjects(arr)); 출력
다음은 콘솔 출력입니다 -
{
name: 'Dinesh Lamba',
age: 23,
occupation: 'Web Developer',
address: 'Vasant Vihar',
experience: 5,
isEmployed: true
}