0

I was trying to merge & add these two objects like this :

obj1 = {
  jpg: 1,
  gif: 3,
}

obj2 = {
  jpg: 1,
  avi: 5,
}

obj3 = mergeAdd(obj1, obj2);
// obj3 = {
//   jpg: 2,
//   gif: 3,
//   avi: 5,
// }

First, I create this which takes O(n^2) likes :

for (let foo in obj1) {
  for (let bar in obj2) {
    if (foo === bar) {
      // Exists : Add count
      obj1[bar] += obj2[bar];
    } else {
      // Don`t Exists : Copy from obj2
      obj1[bar] = obj2[bar];
    }
  }
}

And then, one 'new idea' came into my mind - using hash function :

  1. Get hash value for 'key' of obj1 & obj2.
  2. Insert or add 'value' of obj1 & obj2 to new array using their hash value as an index of the array.
  3. Make the array into new object.

What I want to know :

  1. Is there any library which have function like mergeAdd(obj1, obj2)?
  2. Is my 'new idea' faster than mergeAdd(obj1, obj2)?
  3. What is the fastest algorithm to do mergeAdd?

Thanks!

1
  • O(nlogn) for merging and sorting. Commented Mar 23, 2016 at 14:19

3 Answers 3

3

A solution with linear complexity.

var obj1 = { jpg: 1, gif: 3, },
    obj2 = { jpg: 1, avi: 5, },
    merged = function (array) {
        var r = {};
        array.forEach(function (a) {
            Object.keys(a).forEach(function (k) {
                r[k] = (r[k] || 0) + a[k];
            });
        });
        return r;
    }([obj1, obj2]);

document.write('<pre>' + JSON.stringify(merged, 0, 4) + '</pre>');

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

Comments

1

Are you able to use ES6? you can do this easily with the new Object.assign() method:

let newObj = Object.assign({}, obj1, obj2);

without ES6:

var newObj  = {};
obj1 = {
  jpg: 1,
  gif: 3,
};

obj2 = {
  jpg: 1,
  avi: 5,
};
Object.keys(obj1).forEach(function(k) {
    newObj[k] = obj1[k];
});
Object.keys(obj2).forEach(function(e) {
    newObj[e] = obj2[e];
});

working fiddle: https://jsbin.com/rogada/edit?js,console

taken from: http://es6-features.org/#ObjectPropertyAssignment

Comments

0

A generic recursive/functional solution. Takes an array of objects and merges each one into an output object. No idea how performant it is compared to the other answers though - it's probably pretty bad.

function addValues(obj) {
  return function (p, c) {
    p[c] = (p[c] || 0) + obj[c];
    return p;
  }
}

function mergeObject(obj, out) {
  return Object.keys(obj).reduce(addValues(obj), out);
}

function processArray(arr, fn, out) {
  out = out || {};
  if (!arr.length) return out;
  out = fn(arr.shift(), out);
  return processArray(arr, fn, out);
}

var arr = [{ jpg: 1, gif: 3 }, { jpg: 1, avi: 5, }];
processArray(arr, mergeObject); // { jpg: 2, gif: 3, avi: 5 }

DEMO

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.