1

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.

1
  • Can you show us what you have actually tried and tell us what issue(s) you had with each attempt? Commented Sep 3, 2016 at 12:31

4 Answers 4

1

You requested data format has a lot of redundant information. With due respect i would like to advise you to adjust your code to use a data format as follows;

var data = [{day: "1", id: 3, msg: 'txt'},
            {day: "1", id: 4, msg: 'txt'},
            {day: "2", id: 5, msg: 'txt'},
            {day: "3", id: 6, msg: 'txt'}
           ],
dailyMsg = data.reduce((dm,obj) => dm[obj.day] !== void 0 ? (dm[obj.day] = dm[obj.day].concat({id: obj.id, msg: obj.msg}),dm)
                                                          : (dm[obj.day] = [{id: obj.id, msg: obj.msg}],dm),{});
console.log(dailyMsg);

Sign up to request clarification or add additional context in comments.

Comments

0

You can do this in pure js with forEach() and find()

var ar1 = [{
    day: "1",
    msgs: [{day: "1", id: 1, msg: 'txt'}]
},{
    day: "2",
    msgs: [{day: "2", id: 2, msg: 'txt'}]
}];

var ar2 = [
    {day: "1", id: 3, msg: 'txt'},
    {day: "1", id: 4, msg: 'txt'},
    {day: "2", id: 5, msg: 'txt'},
    {day: "3", id: 6, msg: 'txt'}
];

ar2.forEach(function(o) {
  var e = ar1.find((a) => a.day == o.day);
  (e != undefined) ? e.msgs.push(o) : ar1.push({day: o.day,msgs: [o]});
})

console.log(ar1)

Comments

0

Just follow below steps. Array .concat method will concat the two arrays.

    var arry1= [{
        day: "1",
        msgs: [{day: "1", id: 1, msg: 'txt'}]
    },{
        day: "2",
        msgs: [{day: "2", id: 2, msg: 'txt'}]
    }];

      var array2= [
        {day: "1", id: 3, msg: 'txt'},
        {day: "1", id: 4, msg: 'txt'},
        {day: "2", id: 5, msg: 'txt'},
        {day: "3", id: 6, msg: 'txt'}
    ];

var fullArray= arry1.cancat(array2);

Comments

0

It think Nenad Vracar solution is the best. If the ECMAScript 6 is supported.

My solution does not require: ECMAScript 6

var oldMessages = [
    {
        day: "1",
        msgs: [{day: "1", id: 1, msg: 'txt'}]
    },
    {
        day: "2",
        msgs: [{day: "2", id: 2, msg: 'txt'}]
    }];
    
    var newMessages = [
        {day: "1", id: 3, msg: 'txt'},
        {day: "1", id: 4, msg: 'txt'},
        {day: "2", id: 5, msg: 'txt'},
        {day: "3", id: 6, msg: 'txt'}
    ];
    
    function merge(oldMessages, newMessages) {
      for (var i=0; i<newMessages.length; i++) {
        var exists = false;
        for (var j=0; j<oldMessages.length; j++) {
        	if (oldMessages[j].day == newMessages[i].day) {
          	oldMessages[j].msgs.push(newMessages[i]);
            exists=true;
            break;
          }
        }
        if (!exists) {
        	oldMessages.push({'day':newMessages[i].day,'msgs':newMessages[i]});
        }
      }
      return oldMessages;
    }
    
    console.log(merge(oldMessages, newMessages));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.