Merge Objects by value keeping unique values in an array

I have been attempting to do the below. And I am having difficulty coming up with a clean solution to:

obj1 = {
  key1: "ABC",
  key2: "123",
  key3: "UNIQUE1"
}

obj2 = {
  key1: "ABC",
  key2: "123",
  key3: "UNIQUE2"
}

//I've been trying to merge these to look like the below output

obj3 = {
  key1: "ABC",
  key2: "123",
  key3: ["UNIQUE1", "UNIQUE2"]
}

Essentially I want to keep the unique values per key in an array. I have tried looking at lodash as well and have not been able to come up with a way to do it. Any help would be much appreciated!

5 answers

  • answered 2018-02-13 00:41 Ele

    You can use reduce function and loop over the keys of obj1

    var obj1 = {
      key1: "ABC",
      key2: "123",
      key3: "UNIQUE1",
      key5: "4567",
      key7: "y",
      key8: "8y",
      key9: "yi89",
      key0: "dhdh64",
      key12: "dhdh64",
      key11: "dhdh64"
    }
    
    var obj2 = {
      key1: "ABC",
      key2: "123",
      key3: "UNIQUE2",
      key4: "1212",
      key6: "a",
      key7: "z",
      key11: "z"
    }
    
    var result = Object.keys(obj1).reduce((a, k) => {
      a[k] = obj2[k] === undefined || obj1[k] === obj2[k] ? obj1[k] : [obj1[k], obj2[k]];
      return a;
    }, { ...obj1, ...obj2 });
    
    console.log(result);
    .as-console-wrapper {
      max-height: 100% !important
    }

    Resource

  • answered 2018-02-13 00:41 Reason

    Object.keys(obj1).reduce(function (ack, key) {
        ack[o1key] = (obj1[key] === obj2[key]) 
            ? obj1[key]
            : [obj1[key], obj2[key]];
        return ack;
    }, {})
    

  • answered 2018-02-13 00:41 Nenad Vracar

    You can use mergeWith method and pass empty object as a first parameter to create new object. Then you can check if value of first property is equal to second and based on that create array.

    const obj1 = {
      key1: "ABC",
      key2: "123",
      key3: "UNIQUE1"
    }
    
    const obj2 = {
      key1: "ABC",
      key2: "123",
      key3: "UNIQUE2"
    }
    
    const result = _.mergeWith({}, obj1, obj2, function(a, b) {
      if (a === undefined) return b
      if (a != b) return [].concat(a, b)
    })
    
    console.log(result)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>

  • answered 2018-02-13 00:41 Mamun

    You can loop through object's key with for...in loop to check whether values are equal or not. Try the following:

    var obj1 = {
      key1: "ABC",
      key2: "123",
      key3: "UNIQUE1"
    }
    
    var obj2 = {
      key1: "ABC",
      key2: "123",
      key3: "UNIQUE2"
    }
    var res={};
    for(var key in obj1){
      if(obj1[key] == obj2[key]){
       res[key] = obj1[key];
      }
      else{
        var temp = []; temp.push(obj1[key]); temp.push(obj2[key]);
        res[key] = temp;
      }
    }
    
    console.log(res)

  • answered 2018-02-13 00:41 s1mpl3

    For that particular example

    res = {}
    
    for(i in obj1){
      res[i] = (obj2[i] != obj1[i])? [obj1[i], obj2[i]] :  obj1[i]
    }