Just a few weeks until the 2021 JavaScript Full-Stack Bootcamp opens.
Signup to the waiting list!

If you have an object, you can’t just iterate it using map(), forEach() or a for..of loop.

You will get errors:

const items = {
  'first': new Date(),
  'second': 2,
  'third': 'test'

map() will give you TypeError: items.map is not a function:

items.map(item => {})

forEach() will give you TypeError: items.forEach is not a function:

items.forEach(item => {})

for..of will give you TypeError: items is not iterable:

for (const item of items) {}

So, what can you do to iterate?

for..in is a simpler way:

for (const item in items) {

You can also call Object.entries() to generate an array with all its enumerable properties, and loop through that, using any of the above methods:

Object.entries(items).map(item => {

Object.entries(items).forEach(item => {

for (const item of Object.entries(items)) {

The 2021 JavaScript Full-Stack Bootcamp will start at the end of March 2021. Don't miss this opportunity, signup to the waiting list!

More js tutorials: