How to convert an array into an object?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Converting an array into an object is common in JavaScript when you need key-based lookup instead of index-based access. The best method depends on your input shape: plain value arrays, key-value pairs, or arrays of records. Good conversions are explicit about key generation and collision behavior.
Case 1: Value Array to Index-Keyed Object
If you just want object keys as indices, spread and Object.assign both work.
This preserves all values but keys are strings of numeric indices.
Case 2: Build Object With Custom Keys Using reduce
Most real use cases need semantic keys.
reduce is flexible and clear for key derivation.
Case 3: Array of Pairs With Object.fromEntries
If your array already contains key-value tuples, Object.fromEntries is the cleanest approach.
This is concise and avoids manual loops.
Handling Duplicate Keys Intentionally
When multiple items map to the same key, later values overwrite earlier ones by default.
If you need grouped values, accumulate arrays instead.
Define this behavior up front to avoid silent data loss.
Choosing Object Versus Map
Object is great for JSON-compatible structures and simple lookups. Map is better when:
- key types are not strings.
- insertion order behavior matters strongly.
- you need frequent add and remove operations with large key sets.
Example with Map:
Use object when output must serialize naturally to JSON.
Performance and Memory Notes
Conversion is linear in array length for all common approaches. Performance differences are usually minor compared with correctness and readability. For very large arrays, avoid creating unnecessary intermediate arrays before conversion.
For example, prefer one-pass reduce if you otherwise would map then transform repeatedly.
TypeScript Tip for Safer Conversions
In TypeScript, annotate accumulator type for better safety.
This improves autocomplete and catches key or value shape mistakes.
For API boundary code, validate keys before conversion so malformed records do not silently produce undefined object entries that are difficult to debug later.
Common Pitfalls
A common pitfall is converting arrays to objects without defining key strategy, then discovering unstable or meaningless keys later. Another issue is ignoring duplicate keys and accidentally overwriting values. Teams also use object conversion where Map would better match runtime behavior, especially for non-string keys. Using spread on very large arrays can be convenient but less explicit than purpose-driven reducers. Finally, converting data for lookup without documenting shape can create mismatch between frontend and backend assumptions.
Summary
- Pick conversion strategy based on array shape and lookup requirements.
- Use spread or
Object.assignfor index-keyed conversion. - Use
reduceorObject.fromEntriesfor semantic keys. - Define duplicate-key policy explicitly to avoid silent overwrites.
- Consider
Mapwhen key types or mutation patterns exceed object strengths.
Related reading
- How to convert an array of strings to an array of floats in numpy?
- How to convert an Array to a Set in Java
- How to convert an ArrayList containing Integers to primitive int array?
- How to convert an ArrayList to a strongly typed generic list without using a foreach?
- How to convert Javascript datetime to C datetime?
- How to convert sync and async recursive function to iteration in JavaScript
- How to convert an entire MySQL database characterset and collation to UTF-8?
- How to convert an int array to String with toString method in Java

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.