reduce의 currentvalue에 함수를 넣을때

정태민·2023년 5월 3일
0

자바스크립트

목록 보기
9/12

js 배열 고차함수 reduce에 함수를 배열로 선언해서 넣으면 이런식으로 수행된다.
비동기 함수를 여러개 실행해야할때 쓰면 좋을거같다.
// Define an array of numbers
const numbers = [1, 2, 3, 4, 5];

// Define a function to add two numbers
function Add(arr) {
//console.log("add", value, arr);
return arr.reduce((acc, cv) => acc + cv);
}

// Define a function to multiply two numbers
function Multiply(arr) {
return arr.reduce((acc, cv) => acc * cv);
}

// Define a function that uses reduce to apply a sequence of functions to an array
function applyFunctions(array, functions) {
return functions.reduce((acc, func) => func(array), array);
}
// Call the applyFunctions function with our numbers array and the add and multiply functions
const result = applyFunctions(numbers, [Add, Multiply]);
console.log(result); // Output: 120

profile
퇴근후 30분 출근전 30분

0개의 댓글