'idiomatic way to represent key-value maps in Redux store

I think that most examples I've seen online, when needing to maintain a key-value map in Redux store, they seem to choose to type it as an array of key-value pairs, e.g.:

employees: [{id: 42, emp: {name: 'John Doe'}}, {id: 43, emp: {name: 'Mary Loo'}}]

... typed as:

employees: Array<{id: number, emp: {name: string}}>

However, I would think instead that the following is a tighter typing as it explicitly disallows duplicate id:

employees: Record<id, {name: string}>

Is there a reason for choosing the former over the latter? I understand that Redux does a shallow comparison but I think that when using immutability libraries both array and object structs are equally well supported.



Solution 1:[1]

Not sure why you'd say that "the usual way is to keep it as an array". A normalized lookup table of Record<SomeId, SomeItem> would definitely be the right answer here - a plain JS object where the keys are item IDs and the values are the items themselves.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 markerikson