I'm trying to access the length of the array on which I'm using a reduce function inside that reduce, but I don't seem to be able to do it, does anyone have any idea if it is possible to access the array object inside any of the higher order functions?
PS: I tried using this but to no success;
PSS: I want to calculate the average rating using a reduce function, so I use the reduce to sum all values in the array and divide those same values by the array length, like so:
let averageRating = watchList
.filter(movie => movie.Director === 'Christopher Nolan')
.map(x => parseFloat(x.imdbRating))
.reduce((total, current) => total + (current / 'array length'));
where 'array length' would be, you guessed it, the array length.
PSSS: Tried
var averageRating = watchList
.filter(movie => movie.Director === 'Christopher Nolan')
.map(x => parseFloat(x.imdbRating))
.reduce((total, current, index, arr) => total + (current / arr.length));
but the array length keeps changing as the array is being reduced, so it wouldn't work for my purposes.
reduceis supplied four parameters: the accumulator, the current array value, its index, and the array itself. If you give your callback four parameters, that last one will be the array and you can get its length.