본문 바로가기
웹코딩 배우기/· JavaScript

reduce() - 요소에 함수 걸기

by 닐기 2022. 8. 4.

See the Pen reduce() : 요소에 함수를 실행 by nilgi (@nilgi) on CodePen.

 

 

let plus = no.reduce(myFunction);
function myFunction(total, value, index, array) {
    return total + value;
    }

 

let plus2 = no2.reduce(myFunction, 1000);
function myFunction(total, value) {
    return total + value;
    }

※ 참고
reduce()는 배열 왼쪽에서 오른쪽으로 요소를 호출하고 reduceRight()는 오른쪽에서 왼쪽으로 호출한다.