0

I have the following...

array [
       obj1 = {key1: a,
               key2: [a, b, c]
               },

       obj2 = {key2: [c, d]}
];

And want the following outcome...

array[
       obj1 = {key1: a,
               key2: [a, b, c, d]
               }
]

How can I merge obj1 and obj2 so that key2 contains no duplicates?

Thanks! I am also already using jQuery

user2415131
  • 95
  • 3
  • 9

1 Answers1

0
Array.prototype.unique = function() {
    var a = this.concat();
    for(var i=0; i<a.length; ++i) {
        for(var j=i+1; j<a.length; ++j) {
            if(a[i] === a[j])
                a.splice(j--, 1);
        }
    }

    return a;
};   

var mergedArray = array1.concat(array2).unique();

via this question - How to merge two arrays in Javascript and de-duplicate items

Community
  • 1
  • 1
Jon La Marr
  • 1,358
  • 11
  • 14