DEV Community

Discussion on: .map(), .filter(), and .reduce()

Collapse
 
mateiadrielrafael profile image
Matei Adriel • Edited

Since you understand map but don't understand reduce, implementing map with reduce might help:

const map = (arr, func) => arr.reduce((acc, curr) => [...acc, func(curr)], [])

Edit: here's the same for filter:

const filter = (arr, func) => arr.reduce((acc, curr) => func(curr) ? [...acc, curr] : acc, [])