I have a long list of objects. They look like this:
[{id: 1, brand: 'apple', user:'aa', name:'bb'},
{id: 2, brand: 'apple', user:'cc', name:'dd'},
{id: 3, brand: 'samsung', user:'ee', name:'ff'},
{id: 4, brand: 'samsung', user:'gg', name:'hh'},
{id: 5, brand: 'netflix', user:'ii', name:'jj'},
{id: 6, brand: 'super', user:'kk', name:'ll'},
Using Typescript, I wish to create a dictionary, where the key is brand
, and the value is a list of all the items with that brand. so the result of the list above would be:
{
'apple':[{id: 1, brand: 'apple', user:'aa', name:'bb'}, id: 2, brand: 'apple', user:'cc', name:'dd'}],
'samsung':[{id: 3, brand: 'samsung', user:'ee', name:'ff'}, {id: 4, brand: 'samsung', user:'gg', name:'hh'}],
'netflix': [{id: 5, brand: 'netflix', user:'ii', name:'jj'}],
'super': [{id: 6, brand: 'super', user:'kk', name:'ll'}]
}
The order of the items in the list does not matter. I tried using _.keyBy(store.catalog, 'brand')
but it left me with only one item for each brand, so for this example I lost two objects.