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 ]
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP