1

I have the following two arrays:

var data1=[
    {
        "id": 1,
        "url": "http://192.168.1.165:90/asset/"
    },
    {
        "id": 2,
        "url": "Assigned"
    }


]
var data2=[
    {
        "id": 1,
        "url": "http://192.168.1.165:90/asset/"
    },
    {
        "id": 2,
        "url": "Assigned"
    },
{
        "id": 3,
        "url": "Assigned"
    }

]

Result:

var unique=[{ {
            "id": 3,
            "url": "Assigned"
        }}]

How can I get the unique object from these two arrays ? I have tried using a for loop like this :

var unique = [];
for(var i = 0; i < data2.length; i++){
    var found = false;
    for(var j = 0; data1.length; j++){
     if(data2[i].id == data1[j].id){
      found = true;
      break; 
    }
   }
   if(found == false){
   unique.push(array1[i]);
  }
}

But wanted to get a solution using functional javascript...

forgottofly
  • 2,729
  • 11
  • 51
  • 93
  • Not really a solution, but for all things functional javascript, apparently [lodash](https://lodash.com/) is pretty great...never used it myself, but it might be applicable for you! – jonny Nov 04 '15 at 09:54
  • Possible duplicate of [Union of Array of Objects in JavaScript?](http://stackoverflow.com/questions/13319150/union-of-array-of-objects-in-javascript) – valepu Nov 04 '15 at 10:00

4 Answers4

1

Try like this

var joined = data1.concat(data2);
var temp = [];
joined.forEach(function (x) {
    var objList=joined.filter(function(y){ return y.id == x.id});
    if(objList.length == 1) // if data count of current item in merged array is 1 that's means it belong to only one data source
        temp.push(x);
})

console.log(temp)

JSFIDDLE

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
1

try this: first get the objects from data1 which are not in data2 and remove from data2 if it is there then concat it with data2.

<script>
var data1=[
       {
           "id": 1,
           "url": "http://192.168.1.165:90/asset/"
       },
       {
           "id": 2,
           "url": "Assigned"
       }


   ];
   var data2=[
       {
           "id": 1,
           "url": "http://192.168.1.165:90/asset/"
       },
       {
           "id": 2,
           "url": "Assigned"
       },
   {
           "id": 3,
           "url": "Assigned"
       }

   ];
   
   var arr3 = [];
   for(var i in data1){
      var dup = false;
      for (var j in data2){
       if (data2[j].id == data1[i].id && data2[j].url == data1[i].url) {
             data2.splice(j,1);
          }
      }
      if(dup) arr3.push(arr1[i])
      
   }

   arr3 = arr3.concat(data2);
console.log(arr3);
</script>
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
1

Edited for resulting the single unique object!

Assuming you have a function:

function unique(arr) {
var uni = [];
for(var i=0; i<arr.length; ++i) {
    var rep = -1;
    for(var j=0; j<arr.length; ++j)
        if(arr[i].id == arr[j].id) rep++;
    if (!rep) uni.push(arr[i]);
}
return uni;

}

this would work and give you the single unique object:

var u = unique(data1.concat(data2));
vacsora
  • 222
  • 1
  • 7
1

The idea is to make an union of the two given arrays and then iterate through setA and look for the matching properties and values in setA and in union. If found, then the index is stored. If there are more than one index, delete all items from union whith the indices.

The rest is then the symmetric difference.

var data1 = [{ "id": 1, "url": "http://192.168.1.165:90/asset/" }, { "id": 2, "url": "Assigned" }],
    data2 = [{ "id": 1, "url": "http://192.168.1.165:90/asset/" }, { "id": 2, "url": "Assigned" }, { "id": 3, "url": "Assigned" }];

function symmetricDifference(setA, setB) {
    var union = setA.concat(setB);
    setA.forEach(function (a) {
        var aK = Object.keys(a),
            indices = [];
        union.forEach(function (u, i) {
            var uK = Object.keys(u);
            aK.length === uK.length && 
            aK.every(function (k) { return a[k] === u[k]; }) &&
            indices.push(i);
        });
        if (indices.length > 1) {
            while (indices.length) {
                union.splice(indices.pop(), 1);
            }
        }
    });
    return union;
}
document.write('<pre>' + JSON.stringify(symmetricDifference(data1, data2), 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392