Looking for some assistance with this problem :)
I am trying to convert the object into an array with the following expected result:
result = [
{
id: 'test-1',
message: 'test#1.1'
},
{
id: 'test-1',
message: 'test#1.2'
},
{
id: 'test-2',
message: 'test#2.1'
},
{
id: 'test-2',
message: 'test#2.2'
}
]
I attempted using Object.keys() and map(), but it did not produce the desired outcome as shown below:
mockData = {
'test-1': [
{
message: 'test#1.1'
},
{
message: 'test#1.2'
}
],
'test-2': [
{
message: 'test#2.1'
},
{
message: 'test#2.2'
}
]
}
const result = Object.keys(this.mockData).map((id) => {
return {
id,
...this.mockData[id],
}
})
console.log(result)
Do I need to add another map() over this.mockData[id]? What mistake am I making, and what would be the best practice in this situation (maybe reduce()?)?
Your help is greatly appreciated!