I have one array of messages for example:
[{
day: "1",
msgs: [{day: "1", id: 1, msg: 'txt'}]
},{
day: "2",
msgs: [{day: "2", id: 2, msg: 'txt'}]
}]
and I receive new messages from server in this 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'}
]
And I need to merge new values into old messages array. And this is the example of results I need.
[{
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 spend 5h on stack overflow to find similar case but I did not found it. I've already tried using lodash functions groupBy and forEch and it did not work. The important thing is if NEW DAY comes from server to add that day in old results.