I have the following array of objects:
[
{
message: 'This is a test',
from_user_id: 123,
to_user_id: 567
},
{
message: 'Another test.',
from_user_id: 123,
to_user_id: 567
},
{
message: 'A third test.',
from_user_id: '456',
to_user_id: 567
}
]
How do I construct a new array of objects where the outermost object key is based on a common key found in the original array?
This is what I'm after:
[
{
123: [
{
message: 'This is a test',
from_user_id: 123,
to_user_id: 567
},
{
message: 'Another test.',
from_user_id: 123,
to_user_id: 567
}
]
},
{
456: [
{
message: 'A third test.',
from_user_id: '456',
to_user_id: 567
}
]
}
]
Notice how in the first array, the user ID of 123
shows up in two objects. That would be the object key for the first element in the new array.