如何在另一个函数中使用地图函数和箭头函数?

How to use map function and arrow function inside another function?

我正在尝试通过实现一些函数来获得给定一系列年份的最大心率。 arrayCalc 函数在名为 "arrRes" 的空数组上使用 for 循环来推送新值。同时,calcAge 函数从当年开始计算一个人的年龄。我希望在传递了一些参数的 arrayCalc 函数中使用 .map 和箭头函数,而不是 for 循环。我不知道该去哪里。

我尝试使用 MDN 网络文档等资源来阐明 .map 和箭头的功能。我一知道它的语法和实现,就开始将 .map 和箭头函数包装到一个名为 "arrRes" 的常量变量中。基本上,我试图重现 'old' 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);

我的输出应该是 // [29, 54, 82, 14, 21]。但是控制台给我一个错误"Uncaught TypeError: Cannot read property 'map' of undefined"。显然,我试图实现的代码被注释掉以产生结果。感谢任何帮助。

可以将函数作为参数进行映射。

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; }

或者直接使用 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; }

您缺少 return 函数中的一个值,您还应该 return 执行 fn 函数而不是 arr 的值:

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

工作示例:

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);