2

I got 2 Json arrays with some common field. But they are not sorted in any specific order. I want to be able to merge them based on a property.

var merge = require('deepmerge');
var one = [{
  id:1
},
{
  id:2
}];
var two = [{
  id:2,
  name:"test1"
},
{
  id:1,
  name:"test2"
}];

console.log(merge(one,two));

deepmerge results in blind merge, first element with first element from the other array.

[ { id: 2, name: 'test1' }, { id: 1, name: 'test2' } ]

I know its possible to manually iterate thru one array, find the matching node from the other array and merge them...wondering if there is any library to do this. Thoughts?

7
  • yes, all is possible, but what do you want exactly? Commented Jun 27, 2016 at 11:50
  • @nina I got 2 unsorted arrays but with atleast one common property like I mentioned above and want to merge them. Does that help? Commented Jun 27, 2016 at 11:51
  • not really, please add some more example. and the wanted result of merging. Commented Jun 27, 2016 at 11:53
  • want one merged array as a result, like [ { id: 1, name: 'test2' }, { id: 2, name: 'test1' }, { id: 3, name: 'test3' } ] Commented Jun 27, 2016 at 11:55
  • i see nothing to merge, just take two as result. Commented Jun 27, 2016 at 12:04

3 Answers 3

3

You could use a function and define the common key, on which the merging should group. It works with unsorted data.

function merge(arrays, key) {
    var r = [],
        hash = Object.create(null);

    arrays.forEach(function (a) {
        a.forEach(function (o) {
            if (!hash[o[key]]) {
                hash[o[key]] = {};
                r.push(hash[o[key]]);
            }
            Object.keys(o).forEach(function (k) {
                hash[o[key]][k] = o[k];
            });
        });
    });
    return r;
}

var one = [{ id: 1, score: 100 }, { id: 2, score: 200 }, { id: 4, score: 400 }],
    two = [{ id: 2, name: "test1" }, { id: 1, name: "test2" }, { id: 3, name: "test3" }],
    merged = merge([one, two], 'id');

console.log(merged);

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

Comments

0

Sort both arrays before hand?

one.sort((a, b) => a.id - b.id);
two.sort((a, b) => a.id - b.id);

Now that the arrays are sorted you should be able to merge them properly:

console.log(merge(one, two));

1 Comment

thanks.. I did try this.. it didn't work when I've an array element in 1st array that doesn't have any matching in 2nd. Below is an example Sorted array - one [ { id: 1 }, { id: 2 }, { id: 4 } ] sorted array - two [ { id: 1, name: 'test2' }, { id: 2, name: 'test1' }, { id: 3, name: 'test3' } ] merges array [ { id: 1, name: 'test2' }, { id: 2, name: 'test1' }, { id: 3, name: 'test3' } ]
0

I think you are looking for merge in lodash. Subsequent sources overwrite property assignments of previous sources. So this will overwrite the subsequent id's

var users = {
  'data': [{ 'user': 'barney' }, { 'user': 'fred' }]
};

var ages = {
  'data': [{ 'age': 36 }, { 'age': 40 }]
};

_.merge(users, ages);
// → { 'data': [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }] }

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.