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
constpets=["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`
I appreciate how you've taken this explanation upon yourself. I think there's a revision in here, though;
reduce
is afor
with a return value,map
is a specializedreduce
that gives back a new array.Quite literally, anything you can do with a
for
loop, you can do withreduce
That's an interesting point that I hadn't considered, I'll update the post to reflect that!