Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TypeError when building a Map or object from a mapped or filtered array

ErrorTypeError: Iterator value undefined is not an entry object

Short answer

new Map() and Object.fromEntries() expect every item to be a [key, value] pair. The error means one item was not: usually a map() callback that returned nothing for some items, so the array holds undefined. Return a pair for every item, drop the others with filter(), and remember that Map has no filter method.

  • Applies to Node.js 22, Node.js 24, JavaScript ES2025
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log the array just before it goes into new Map(). Look for items that are undefined or not two-element arrays.

    console.log(items.filter((e) => !Array.isArray(e) || e.length !== 2));
  2. 2Match what you see, then jump to its fix

    1. 1The array logged above contains undefined entries; the callback has an if without a matching returnA map() callback returns nothing for some itemsFix
    2. 2The array logged above holds strings, numbers or plain objects instead of two-element arraysThe items are values, not [key, value] pairsFix
    3. 3Code like new Map(oldMap.filter(...)) or a loop that yields only the value, so the pairs are lostYou tried to filter a Map directlyFix

What does the error look like?

  • TypeError: Iterator value undefined is not an entry object

Cause 1 of 3

A map() callback returns nothing for some items

1Check

MDN documents that when a map() callback returns undefined or nothing, the new array contains undefined. new Map() requires each element to be an object with properties 0 and 1, so an undefined element is rejected.

const pairs = list.map((x) => {
  if (x.active) return [x.id, x];
});
console.log(pairs); // [ [1, {...}], undefined, ... ]

2Fix

Remove the unwanted items first with filter(), or use flatMap() and return an empty array for the items to skip.

const m = new Map(
  list.filter((x) => x.active).map((x) => [x.id, x])
);

// or
const m2 = new Map(
  list.flatMap((x) => (x.active ? [[x.id, x]] : []))
);

3Verify

Build the Map again and print its size.

console.log(m.size);

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 3

The items are values, not [key, value] pairs

1Check

The Map documentation says each element must be an object with two properties, 0 and 1, the key and the value. Object.fromEntries has the same rule, with 0 as the property key and 1 as the value.

console.log(Array.isArray(items[0]), items[0]);

2Fix

Turn each item into a pair before building the Map or object.

const byId = new Map(users.map((u) => [u.id, u]));
const obj = Object.fromEntries(users.map((u) => [u.name, u.age]));

3Verify

Read one value back.

console.log(byId.get(users[0].id));

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 3

You tried to filter a Map directly

1Check

MDN lists no filter or map method on Map. Its iterators yield [key, value] arrays, and the documented way to use array methods is to spread the Map into an array first. Filtering on the value alone loses the key.

console.log([...oldMap][0]);

2Fix

Spread the Map into an array of pairs, filter and map, then build a new Map from the result.

const result = new Map(
  [...scores]
    .filter(([key, value]) => value > 3)
    .map(([key, value]) => [key, value * 10])
);

3Verify

Print the new Map.

console.log(result);

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Return a value from every branch of a map() callback, or filter first.
  • Keep the pair shape [key, value] visible in the callback so it is obvious what goes into new Map().
  • Use Object.fromEntries and new Map with the spread-and-filter pattern instead of deleting keys in a loop.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

The AWS SDK in Node.js cannot find any credentials

Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

The AWS SDK walks a fixed credential chain: code, environment variables, the shared files in ~/.aws, then role sources such as ECS or EC2.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.