简体   繁体   English

如何在另一个函数中使用map函数和arrow函数?

[英]How to use map function and arrow function inside another function?

I'm trying to obtain the max heart rate given an array of years by implementing a few functions. 我正在尝试通过实现一些功能来获取给定年份的最大心率。 The arrayCalc function uses a for loop on a empty array called "arrRes" to push a new value. arrayCalc函数在名为“ arrRes”的空数组上使用for循环来推送新值。 At the same time, the calcAge function calculates the age of a person from the current year. 同时,calcAge函数根据当前年份计算一个人的年龄。 Instead of the for loop I wish to use the .map and the arrow functions inside of the arrayCalc function which has some arguments passed. 我希望使用传递了一些参数的arrayCalc函数内部的.map和arrow函数,而不是for循环。 I do not know where to go about this. 我不知道该去哪里。

I've tried using resources like MDN web doc to clarify what a .map and arrow functions. 我尝试使用MDN网络文档等资源来阐明.map和arrow功能。 As soon as I know its syntax and its implementation, I started to wrap the .map and the arrow function into a constant variable called "arrRes". 我一知道它的语法及其实现,就开始将.map和arrow函数包装到一个称为“ arrRes”的常量变量中。 Basically, I'm trying to reproduce the same result given in the 'old' arrayCalc. 基本上,我正在尝试重现“旧” arrayCalc中给出的相同结果。

 const years = [1990, 1965, 1937, 2005, 1998]; // The function I'm trying to replicate function arrayCalc(arr, fn) { const arrRes = []; for (let i = 0; i < arr.length; i++) { arrRes.push(fn(arr[i])); } return arrRes; } // My attempt to shorten the arrayCalc function using .map and the arrow /* function arrayCalc(arr, fn){ const arrRes = arr.map(arry => arry); } */ function calcAge(ex) { return new Date().getFullYear() - ex; } function maxHeartRate(ex) { const result_2 = Math.round(206.9 - (0.67 * ex)) return (ex >= 18 && ex <= 81 ? result_2 : -1) } const ages = arrayCalc(years, calcAge); const heartRate = arrayCalc(ages, maxHeartRate); console.log(ages); console.log(heartRate); 

My output should be // [29, 54, 82, 14, 21]. 我的输出应为// [29,54,82,14,21]。 But the console is given me an error "Uncaught TypeError: Cannot read property 'map' of undefined". 但是控制台给我一个错误“未捕获的TypeError:无法读取未定义的属性'map'”。 Obviously, the code I'm trying to implement is commented out to produce the result. 显然,我尝试实现的代码被注释掉以产生结果。 Any help is appreciated. 任何帮助表示赞赏。

You could take the function as parameter for mapping. 您可以将该函数作为映射参数。

function arrayCalc(arr, fn) {
    return arr.map(fn);
}

 const years = [1990, 1965, 1937, 2005, 1998]; function arrayCalc(arr, fn) { return arr.map(fn); } function calcAge(ex) { return new Date().getFullYear() - ex; } function maxHeartRate(ex) { const result_2 = Math.round(206.9 - (0.67 * ex)) return (ex >= 18 && ex <= 81 ? result_2 : -1) } const ages = arrayCalc(years, calcAge); const heartRate = arrayCalc(ages, maxHeartRate); console.log(ages); console.log(heartRate); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Or take a shorter apporoach using Array#map directly with the callback. 或者直接在回调中使用Array#map来缩短时间。

 function calcAge(ex) { return new Date().getFullYear() - ex; } function maxHeartRate(ex) { const result_2 = Math.round(206.9 - (0.67 * ex)) return ex >= 18 && ex <= 81 ? result_2 : -1; } const years = [1990, 1965, 1937, 2005, 1998]; const ages = years.map(calcAge); const heartRate = ages.map(maxHeartRate); console.log(ages); console.log(heartRate); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You have missing to return a value from your function and also you should return the value from executing fn function and not arr : 您缺少从函数中返回值的方法,并且还应该从执行fn函数而不是arr返回值:

function arrayCalc(arr, fn){
  const arrRes = arr.map(a => fn(a)); // and not .map(arr => arr)
  return arrRes; // missing return statement
}

Working example : 工作示例

 const years = [1990, 1965, 1937, 2005, 1998]; // The function I'm trying to replicate /*function arrayCalc(arr, fn) { const arrRes = []; for (let i = 0; i < arr.length; i++) { arrRes.push(fn(arr[i])); } return arrRes; }*/ // My attempt to shorten the arrayCalc function using .map and the arrow function arrayCalc(arr, fn){ const arrRes = arr.map(a => fn(a)); return arrRes; // OR // return arr.map(fn); } function calcAge(ex) { return new Date().getFullYear() - ex; } function maxHeartRate(ex) { const result_2 = Math.round(206.9 - (0.67 * ex)) return (ex >= 18 && ex <= 81 ? result_2 : -1) } const ages = arrayCalc(years, calcAge); const heartRate = arrayCalc(ages, maxHeartRate); console.log(ages); console.log(heartRate); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM