There are countless ways to iterate through JS objects and arrays. Here’s how to do it in the simplest way, with ES6 and beyond, for each purpose.
Keys and values
Loop through keys and values of an object.
Loop through keys of an object.
Loop through values of an object.
Loop through an array.
If you want to
return the items or the whole loop, you have to swap the
forEach for a
map. You can change it in any of the examples above, it will work as expected, and the result will be returned. A
forEach will always return