StackDevLife
1-Min FixBeginnerJavaScript / React / Next.js / Angular

Fix “map is not a function” Quickly

~1 min read
TL;DR

This usually happens when you think a value is an array, but it is actually undefined, null, or an object.

If you get:

map is not a function

the problem is usually not with map itself.

The real issue is that the value before .map() is not an array.

Quick fix checklist:

  1. Log the value before mapping
  2. Confirm the API really returns an array
  3. Add a safe fallback

Example:
const items = Array.isArray(data) ? data : [];
items.map(item => ...)

Common causes:

  • API returned an object instead of a list
  • Initial state was set to null or {}
  • Nested response path was wrong
  • Data was not loaded yet
Safe default:
Always initialize list state as [] when you expect arrays.
That one small default fixes a surprising number of UI crashes.
JavaScriptFrontendReactAngularDebugging

Get a new 1-Min Fix in your inbox

Subscribe to Stack Dev Life — free, no spam, unsubscribe anytime.

Subscribe free →