Given the following structure I’m trying to remove and flatten the array of objects to be the same as the objectArrayResult
below.
const objectArray = [
{
"dynamicID1": {
'key1': 'value1'
},
"dynamicID2": {
'key2': 'value2'
},
}
]
The result should be:
const objectArrayResult = [
{
'key1': 'value1'
},
{
'key2': 'value2'
}
]
2
Answers
First, you need to get all the "objects" under "dynamic" fields. Then, for each of these values of dynamic keys, we need to iterate over their props and put it in new object.
Try this:
EDIT
Here’s one liner suggested in comments:
You can use
Object.values
to get an array of the values of each object, flattening the result withArray#flatMap
.