loading...

Discussion on: Map, Filter, & Reduce in JavaScript

Collapse
thepeoplesbourgeois profile image
Josh

I appreciate how you've taken this explanation upon yourself. I think there's a revision in here, though; reduce is a for with a return value, map is a specialized reduce that gives back a new array.

Quite literally, anything you can do with a for loop, you can do with reduce

const pets = ["cat", "dog", "birb"]

pets.reduce((item, acc) => 
  console.log(item),
  null
) // effectively a logging `for`

pets.reduce((item, acc) => 
  /(cat|dog)/.test(item) ? [...acc, item] : acc,
  []
) // self-implemented `filter`

pets.reduce((item, acc) =>
  [...acc, `cute ${item}`],
  []
) // self-implemented `map`
Collapse
man2006 profile image
Muhannad Author

That's an interesting point that I hadn't considered, I'll update the post to reflect that!