TypeError when building a Map or object from a mapped or filtered array
TypeError: Iterator value undefined is not an entry objectShort 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.
Fix it fast
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));2Match what you see, then jump to its fix
- 1
The array logged above contains undefined entries; the callback has an if without a matching returnA map() callback returns nothing for some itemsFix - 2
The array logged above holds strings, numbers or plain objects instead of two-element arraysThe items are values, not [key, value] pairsFix - 3
Code like new Map(oldMap.filter(...)) or a loop that yields only the value, so the pairs are lostYou tried to filter a Map directlyFix
- 1
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
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
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
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 fixBack end
AWS SDK for JavaScript v3 cannot find credentials, even for DynamoDB Local
A DynamoDB client pointed at http://localhost:8000 fails with the same message
The SDK walked its default credential provider chain and found nothing.
3 causes, each with a fixBack end
AWS SDK says the security token included in the request is invalid
The security token included in the request is invalid
The call reached AWS but the credentials it carried were refused.
3 causes, each with a fixBack end
TypeError: Cannot convert object to primitive value in JavaScript
TypeError: Cannot convert object to primitive value
JavaScript turns an object into a string or number by calling Symbol.toPrimitive, then valueOf and toString.
2 causes, each with a fixBack end
Amazon Cognito says the user pool client does not exist
ResourceNotFoundException (HTTP status code 400) returned by InitiateAuth
Cognito looks the app client ID up inside the Region you call.
3 causes, each with a fixBack end
Browser does not reload when I save a Blade file under Vite
CSS and JavaScript hot update, but view changes need a manual refresh
Vite only reloads the page for Blade files when the Laravel plugin's refresh option is on.
3 causes, each with a fix