小编edm*_*ani的帖子

访问更高阶函数内的数组对象

我正在尝试访问我在其中使用reduce函数的数组的长度,但是我似乎无法做到,有没有人知道是否可以访问数组任何高阶函数内的对象?

PS:我试过this但没有成功;

PSS:我想使用reduce函数计算平均评级,所以我使用reduce来对数组中的所有值求和,并将这些相同的值除以数组长度,如下所示:

let averageRating = watchList
    .filter(movie => movie.Director === 'Christopher Nolan')
    .map(x => parseFloat(x.imdbRating))
    .reduce((total, current) => total + (current / 'array length'));
Run Code Online (Sandbox Code Playgroud)

你猜对了'数组长度',数组长度.

PSSS:试过

var averageRating = watchList
  .filter(movie => movie.Director === 'Christopher Nolan')
  .map(x => parseFloat(x.imdbRating))
  .reduce((total, current, index, arr) => total + (current / arr.length));
Run Code Online (Sandbox Code Playgroud)

但是数组长度随着数组的减少而不断变化,所以它不适用于我的目的.

javascript arrays higher-order-functions

7
推荐指数
1
解决办法
242
查看次数