Suppose I have an array of messages structured like this:
[{
day: "1",
msgs: [{day: "1", id: 1, msg: 'txt'}]
},{
day: "2",
msgs: [{day: "2", id: 2, msg: 'txt'}]
}]
Now, new messages are received from the server in the following format:
[
{day: "1", id: 3, msg: 'txt'},
{day: "1", id: 4, msg: 'txt'},
{day: "2", id: 5, msg: 'txt'},
{day: "3", id: 6, msg: 'txt'}
]
The task at hand is to merge these new values into the existing messages array. The desired outcome should resemble the example below:
[{
day: "1",
msgs: [{day: "1", id: 1, msg: 'txt'},
{day: "1", id: 3, msg: 'txt'},
{day: "1", id: 4, msg: 'txt'}]
},{
day: "2",
msgs: [{day: "2", id: 2, msg: 'txt'},
{day: "2", id: 5, msg: 'txt'}]
},{
day: "3",
msgs: [{day: "3", id: 6, msg: 'txt'}]
}]
I've spent hours searching on stack overflow for a similar scenario but couldn't find one. I attempted using lodash functions like groupBy and forEach, but unfortunately, none of them worked as expected. The crucial requirement is that if a NEW DAY is introduced by the server, it should be added to the existing results.