DEV Community

Discussion on: Use this trick to map over single objects in Javascript

Collapse
 
allnulled profile image
allnulled • Edited

Remember you can, in most of the cases, simply do:

[].concat(itemOrItems).forEach(console.log)
Enter fullscreen mode Exit fullscreen mode

This way, you only need to avoid undefined or null values.

Collapse
 
arikaturika profile image
Arika O • Edited

Why use forEach() instead of map()? ForEach returns undefined so you can't display anything on the page when looping through the array.

Collapse
 
allnulled profile image
allnulled

Remember that the typical functions in JavaScript functional programming nowadays, are: forEach, filter, map, reduce (the last one is the more powerful of all).

We also have others, like: Object.assign and Object.defineProperty.

I mentioned forEach because it is the simplest, not because it fit in the example.

Thank you.

Thread Thread
 
arikaturika profile image
Arika O

Thank you for your input.