I got an object which contains some values.
How do i transform this properly?
I tried multiple things like includes(), find(), some(), for-loops, but it seems like the check if 'conversationWith' is included in the object is failing.
The object looks like this:
{
_id: 5d3eed4b8558ab0fc513a3b5,
subject: 'Subject',
message: 'Message',
newMessage: true,
from: 5d3b0585181c521610a15241,
fromName: 'John Doe',
to: 5d3b0749c9b633171fa62a48,
toName: 'Leeroy Jenkins',
conversationWith: 'Leeroy Jenkins'
}
{
_id: 5d3eed608558ab0fc513a3b7,
subject: '2nd',
message: '2nd',
newMessage: true,
from: 5d3b0585181c521610a15241,
fromName: 'John Doe',
to: 5d3b0749c9b633171fa62a48,
toName: 'Leeroy Jenkins',
conversationWith: 'Leeroy Jenkins'
}
{
_id: 5d3ef15e6a570c1201457918,
subject: '3rd',
message: '3rd',
newMessage: true,
from: 5d3b0585181c521610a15241,
fromName: 'John Doe',
to: 5d3b0749c9b633171fa62a48,
toName: 'Leeroy Jenkins',
conversationWith: 'Leeroy Jenkins'
}
I want to create an array like this out of it:
[{
conversationWith: "Leeroy Jenkins",
message: ["Message", "2nd", "3rd"]
}]
The Problem is that i have multiple objects that I'm looping through, where I only want the 'conversationWith' property to be inserted into the array once, while i want all the messages in it.
I either get the 'conversationWith' property inserted multiple times or not at all.
conversationWithinside the newly created objects, then you will have to loop through them all the time to figure out if you got one with the currentconversationWithalready. I’d recommend to useconversationWithas a key to begin with, that makes these “look ups” easier.