When using the Lodash _.groupBy method for object keys, I want to save the keys.
Suppose I have an object:
foods = { apple: { type: 'fruit', value: 0 }, banana: { type: 'fruit', value: 1 }, broccoli: { type: 'vegetable', value: 2 } }
I would like to make a conversion to get a conclusion
transformedFood = { fruit: { apple: { type: 'fruit', value: 0 }, banana: { type: 'fruit', value: 1 } }, vegetable: { broccoli: { type: 'vegetable', value: 2 } } }
Running transformedFood = _.groupBy(foods, 'type') gives the following result:
transformedFood = { fruit: { { type: 'fruit', value: 0 }, { type: 'fruit', value: 1 } }, vegetable: { { type: 'vegetable', value: 2 } } }
Please note that the original keys are lost. Does anyone know of an elegant way to do this, ideally in a single lodash line feature?
source share