"JavaScript 배열 reduce()"의 두 판 사이의 차이

6번째 줄: 6번째 줄:
   return total + num;
   return total + num;
});
});
console.log( result );
console.log( result ); // 130 = 100 + 20 + 10
</syntaxhighlight>
<syntaxhighlight lang='javascript' run>
const numbers = [100, 20, 10];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
console.log(numbers.reduce(reducer));  // 130 = 100 + 20 + 10
console.log(numbers.reduce(reducer, 5)); // 135 = 5 + 100 + 20 + 10
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang='javascript' run>
<syntaxhighlight lang='javascript' run>
13번째 줄: 19번째 줄:
   return total - num;
   return total - num;
});
});
console.log( result );
console.log( result ); // 70 = 100 - 20 - 10
</syntaxhighlight>
</syntaxhighlight>



2021년 5월 2일 (일) 15:20 판

1 개요

const numbers = [100, 20, 10];
const result = numbers.reduce(function(total, num) {
  return total + num;
});
console.log( result ); // 130 = 100 + 20 + 10
const numbers = [100, 20, 10];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
console.log(numbers.reduce(reducer));   // 130 = 100 + 20 + 10
console.log(numbers.reduce(reducer, 5)); // 135 = 5 + 100 + 20 + 10
const numbers = [100, 20, 10];
const result = numbers.reduce(function(total, num) {
  return total - num;
});
console.log( result ); // 70 = 100 - 20 - 10

2 같이 보기

3 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}