I need some help solving this :)
I want to transfer the object to an array. The expected result should be:
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'
}
]
My apparent solution is with objects.keys() and map(). Unfortunately, this does not work as desired:
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 have to put another map() over this.mockData[id]? what am I doing wrong and what is best practice here (maybe reduce()?)?
I hope you can help me