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...

2
  • Not really a solution, but for all things functional javascript, apparently lodash is pretty great...never used it myself, but it might be applicable for you! Commented Nov 4, 2015 at 9:54
  • Possible duplicate of Union of Array of Objects in JavaScript? Commented Nov 4, 2015 at 10:00

4 Answers 4

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

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

2 Comments

did you checked my fiddle ? @forgottofly
Yeah,I'm getting 3 objects
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>

2 Comments

@forgottofly the updated answer will give only one record for current data with id=3.
Thanks very much @Suchit
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));

Comments

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>');

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.