I'm banging my head against a well trying to do the following with lodash. I have an array of objects with more nested objects, which looks like this:
[{
id: 123,
name: 'John',
summary1: {
count: 3,
sum: 10,
},
summary2: {
count: 10,
sum: 20
},
},
...
]
I want to convert each element of this array into something like this:
[{
id: 123,
name: 'John',
summary1_count: 3,
summary1_sum: 10
summary2_count: 10,
summary2_sum: 20,
},
...
]
SO basically I want to "flatten" each element of the array, in a way such that object keys are determined based on the main and subkeys. How can I accomplish this using lodash or plain JS?
You can assume there's just 1 level of nesting like in the example.
typeof null
is stillobject
, you may want to add this to the condition as well to avoid bugs – Usa