TryBugHunt

TypeError: data.map is not a function

TypeError: data.map is not a function in JavaScript means the value exists, but it is not an array. Usually an object wrapping the array, a JSON string you have not parsed, or a NodeList.

JavaScriptPattern: Type errors

How do I fix TypeError: data.map is not a function?

TypeError: data.map is not a function means the value exists but is not an array. Often the API sent an object like { items: [...] }, you have a JSON string you never parsed, or it is a NodeList or plain object. Log Array.isArray(data), then use data.items, JSON.parse(), Object.values() or Array.from() to get an array.

  1. 1. Log the typeconsole.log(typeof data, Array.isArray(data), data) shows what the value really is.
  2. 2. Look inside the objectIf data is an object, the array is usually a property such as data.items or data.results.
  3. 3. Parse or convert itStrings from localStorage need JSON.parse(). NodeLists need Array.from(). Objects need Object.values() or Object.entries().

What does TypeError: data.map is not a function mean?

map lives on Array.prototype only. The value is defined — otherwise the error would be "Cannot read properties of undefined" — but it is some other type. console.log(typeof data, Array.isArray(data)) answers it in one line.

Why does TypeError: data.map is not a function happen?

1. The API returned an object, not an array

Most APIs send { data: [...] } or { items: [...], total: 50 }.

Breaks

const res = await fetch("/api/users");
const users = await res.json();
users.map(u => u.name);

Works

const { items } = await res.json();
items.map(u => u.name);

2. An object you meant to iterate

Plain objects have no map. Object.values, keys or entries give you an array.

Breaks

const scores = { ada: 3, bo: 5 };
scores.map(s => s * 2);

Works

Object.entries(scores).map(([name, s]) => [name, s * 2]);

3. A JSON string that was never parsed

localStorage and some fetch wrappers hand back strings. Strings have no map.

Breaks

const todos = localStorage.getItem("todos");
todos.map(t => t.title);

Works

const todos = JSON.parse(localStorage.getItem("todos") ?? "[]");
todos.map(t => t.title);

4. A NodeList or Set

querySelectorAll returns a NodeList, which has forEach but not map. Sets are the same: forEach, but no map.

Breaks

document.querySelectorAll("li").map(li => li.textContent);

Works

Array.from(document.querySelectorAll("li"), li => li.textContent);

How to find it in your own code

Log Array.isArray(value) and the value itself. Then either reach into it for the actual array, convert it (Array.from, Object.values, JSON.parse), or fix the producer so it always returns an array.

Practise type errors

None of the challenges raises this exact error yet. These practise the same kind of bug.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Related error guides

View all type errors →