I've searched extensively for a solution to this dilemma, but have had no luck so far. Therefore, I am turning to the community for help. Please feel free to direct me to any existing similar queries.
My challenge involves working with an array of functions of unknown length (n). My goal is to execute all the methods and then aggregate the results of each function. While I'm aware that this can be achieved using both Array.prototype.map
and Array.prototype.reduce
, as shown below:
const arr = [
() => 4,
() => 5,
() => 2,
];
const result = arr
.map((f) => f())
.reduce((acc, next) => acc + next);
// result = 11
This approach is already quite elegant, but I am curious if there exists an even more concise method utilizing just one array function. Ideally, I am looking for a native solution that could potentially incorporate custom types when used with TypeScript.