Object integer key insertion order surprise
Quick summary
You added keys '2', '1', '10' to an object expecting insertion order, but they come back as '1', '2', '10' numerically.
Why this happens
Assuming Object.keys preserves insertion order for integer-like keys
Using object keys as an ordered map for numeric IDs
Mixing integer and string keys and expecting consistent order
Minimal example
const obj = {};
obj['2'] = 'b';
obj['1'] = 'a';
obj['10'] = 'c';
console.log(Object.keys(obj)); // ['1','2','10'] — numeric order, not insertionconst map = new Map();
map.set('2', 'b');
map.set('1', 'a');
map.set('10', 'c');
console.log([...map.keys()]); // ['2','1','10'] — insertion order preservedInteger-like object keys are reordered numerically by the engine. A Map preserves insertion order for all keys, giving the expected sequence.
How to diagnose
Print Object.keys(obj) and compare with insertion order
Check whether keys are integer-like (canonical numeric index strings)
Use a Map if insertion order matters for numeric keys
How to fix
Use a Map which preserves insertion order for all key types
Use an array of { key, value } pairs if order matters
Prefix integer keys with a non-numeric string to force string ordering
How to prevent
Use Map for ordered key-value storage
Never rely on object key order for integer-like keys