5

I need to merge 2 objects with nested arrays

var dest = {
  id: "865",
  arr: [{
    id: "123",
    value: "First" }]
};

var src = {
  id: "865",
  arr: [{
    id: "456",
    value: "Second" }]

};

to produce

merge = {
  id: "865",
  arr: [{id: "123",
         value: "First"},
        {id: "456",
         value: "Second"}]
};

I tried using _.merge(dest, src) (using Lodash) and a couple of other methods, but seems like the 2nd object is overwriting the first one because it doesn't handle the nested array the way I want.

What is the best way to do this?

Thanks,

1
  • dest.arr = _.merge(dest.arr,src.arr) Commented Oct 13, 2016 at 17:14

3 Answers 3

5

You can use Lodash _.mergeWith method:

var dest = {
  id: "865",
  arr: [{
    id: "123",
    value: "First"
  }]
};

var src = {
  id: "865",
  arr: [{
    id: "456",
    value: "Second"
  }]

};

var merge = _.mergeWith({}, src, dest, function(a, b) {
  if (_.isArray(a)) {
    return b.concat(a);
  }
});
console.log(merge);

It allows you to pass a customizer in order to merge the array in a "custom" way.

Here's the fiddle. Hope it helps.

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

3 Comments

Thanks! This was what I was looking for.
@GavinAng Glad to hear it. Please, accept the answer if it's ok with you, thanks!
how would this have to change if I need to merge based on the id field in top level (ie. id=865). I realize the above doesn't work if both the src and dest are arrays of objects and I am trying to merge based on id.
0

You can use Object.assign()

var dest = {
  id: "865",
  arr: [{
    id: "123",
    value: "First" }]
};

var src = {
  id: "865",
  arr: [{
    id: "456",
    value: "Second" }]

};

var merge = Object.assign({}, dest);
merge.arr.push(Object.assign({}, src.arr[0]));

src.arr[0].id = 789; // should not affect `merge.arr`

console.log(merge);

Comments

0

Without any libraries.

    var dest = {
      id: "865",
      arr: [{
        id: "123",
        value: "First"
      }]
    };

    var src = {
      id: "865",
      arr: [{
        id: "456",
        value: "Second"
      }]

    };

    // 1
    var resultOne = {
      id: dest.id,
      arr: src.arr.concat(dest.arr)
    };

    // 2
    var resultTwo = Object.assign({}, src, {
      arr: src.arr.concat(dest.arr)
    });


    // 3
    var merge = function(obj1, obj2) {
      return Object.keys(obj1).reduce(function(result, next) {
        if (Array.isArray(obj1[next]) && Array.isArray(obj2[next])) {
          result[next] = obj1[next].concat(obj2[next]);
        } else if (obj1[next] && obj2[next]) {
          result[next] = obj2[next];
        }
        return result;
      }, {});
    }

    console.log(merge(src, dest));

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.