Reputation: 343
I am creating a helper function to convert a Object of data into an array using javascript. Here is a sample of the data that I want to convert.
const DATA = {
{
title: 'Jun 23, 2021',
data: [
{
messageSentTime: '6:32 PM',
senderAvatar: 'okef8ia9fkil3drzxemy',
senderMessage: 'FsvaevseVefwe'
}
]
},
{
title: 'Jun 23, 2021',
data: [
{
messageSentTime: '6:32 PM',
senderAvatar: 'okef8ia9fkil3drzxemy',
senderMessage: 'FsvaevseVefwe'
}
]
},
}
Here is what I want to update the data to.
const DATA = [
{
title: 'Jun 23, 2021',
data: [
{
messageSentTime: '6:32 PM',
senderAvatar: 'okef8ia9fkil3drzxemy',
senderMessage: 'FsvaevseVefwe'
}
]
},
{
title: 'Jun 23, 2021',
data: [
{
messageSentTime: '6:32 PM',
senderAvatar: 'okef8ia9fkil3drzxemy',
senderMessage: 'FsvaevseVefwe'
}
]
},
]
Here is what I tried to convert the data.
Object.keys(dateOfConversation).map(i => dateOfConversation[i])
I am guessing I need to use the reduce method, and I do not know how to use it. Any help would be greatly appreciated.
Upvotes: 0
Views: 32
Reputation: 1560
Your object example is wrong.
I'll go on a limb here and give out a solution while fixing the above stated example:
const DATA = {
key1: {
title: 'Jun 23, 2021',
data: [
{
messageSentTime: '6:32 PM',
senderAvatar: 'okef8ia9fkil3drzxemy',
senderMessage: 'FsvaevseVefwe'
}
]
},
key2: {
title: 'Jun 23, 2021',
data: [
{
messageSentTime: '6:32 PM',
senderAvatar: 'okef8ia9fkil3drzxemy',
senderMessage: 'FsvaevseVefwe'
}
]
},
}
// solution
const arr = Object.values(DATA);
console.log(arr);
Upvotes: 1