JavaScript 中乘以陣列元素的 Currified 函式


問題

我們需要編寫一個 JavaScript 函式,該函式接收一個數組並返回另一個函式,該函式又接收一個數字,該數字返回一個新陣列,它是輸入陣列的對應元素與提供給第二個函式的數字的乘積。

示例

以下是程式碼 -

 線上演示

const arr = [2, 5, 2, 7, 8, 4];
const num = 4;
const produceWith = (arr = []) => (num) => {
   const res = arr.map(el => {
      return el * num;
   });
   return res;
};
console.log(produceWith(arr)(num));

輸出

以下是控制檯輸出 -

[ 8, 20, 8, 28, 32, 16 ]

更新於: 17-Apr-2021

168 次瀏覽

開始你的 職業生涯

完成教程以獲得認證

立即開始
廣告
© . All rights reserved.