다음과 같은 객체 배열이 있다고 가정해 보겠습니다. -
const arr = [
{
duration: 10,
any: 'fields'
}, {
duration: 20,
any: 'other fields'
}, {
duration: 15,
any: 'some other fields'
}
]; 우리는 그러한 배열 하나를 취하고 모든 객체의 지속 시간 속성의 합산 결과를 반환하는 JavaScript 함수를 작성해야 합니다.
위 배열의 경우 출력은 45여야 합니다.
예시
이에 대한 코드는 -
const arr = [
{
duration: 10,
any: 'fields'
}, {
duration: 20,
any: 'other fields'
}, {
duration: 15,
any: 'some other fields'
}
];
const addDuration = arr => {
let res = 0;
for(let i = 0; i < arr.length; i++){
res += arr[i].duration;
};
return res;
};
console.log(addDuration(arr)); 출력
콘솔의 출력 -
45