0

I have an array containing arrays of objects which I need to compare.

I've looked through multiple similar threads, but I couldn't find a proper one that compares multiple arrays of objects (most are comparing two arrays of objects or just comparing the objects within a single array)

This is the data (below is a JSFiddle with code sample)

const data = [  
  [  
    {  
      "id": "65",
      "name": "Some object name",
      "value": 90
    },
    {  
      "id": "89",
      "name": "Second Item",
      "value": 20
    }
  ],
  [  
    {  
      "id": "89",
      "name": "Second Item",
      "value": 20
    },
    {  
      "id": "65",
      "name": "Some object name",
      "value": 90
    }
  ],
  [  
    {  
      "id": "14",
      "name": "Third one",
      "value": 10
    }
  ]
]

I want to remove all duplicate arrays of objects, regardless of the length of data (there could be a lot more records).

I managed to get the unique ones extracted into an object:

const unique = data.reduce(function(result, obj) {
    return Object.assign(result, obj)
}, [])

That doesn't work for me though, because I need 1 of the duplicated arrays to remain and the returned data to be an array as well, instead of an object. E.g.:

// result I need
[  
  [  
    {  
      "id":"65",
      "name":"Some object name",
      "value":90
    },
    {  
      "id":"89",
      "name":"Second Item",
      "value":20
    }
  ],
  [  
    {  
      "id":"14",
      "name":"Third one",
      "value":10
    }
  ]
]

So how do I compare each array of objects to the others in the parent array and preserve one of each duplicated or unique array of objects?

JSFiddle

abpetkov
  • 884
  • 5
  • 11
  • 29
  • The concept of searching for a duplicate of an object remains the same, no matter if you're searching in one or in multiple arrays. – Cerbrus Dec 13 '17 at 09:11
  • Looks more complex as a matter of fact, otherwise I would've probably done it by now based on the hundreds of answers to **simply** filter out duplicate objects in a single array. Same goes for the question you linked, which won't satisfy this use case. – abpetkov Dec 13 '17 at 09:18
  • I'm not sure this is a duplicate of the one linked as the inner arrays appear to be in order, so they need to be sorted before they can be compared. I've put together a solution using some lodash functions - `uniqWith` and `sortBy`: https://jsbin.com/terelef/2/edit?js,console – grahamlyons Dec 13 '17 at 09:41

2 Answers2

0

you can achieve so by using function.As below. Not sure about best optimum way of doing so.

var testArray = [  
  [  
    {  
      "id": "65",
      "name": "Some object name",
      "value": 90
    },
    {  
      "id": "89",
      "name": "Second Item",
      "value": 20
    }
  ],
  [  
    {  
      "id": "89",
      "name": "Second Item",
      "value": 20
    },
    {  
      "id": "65",
      "name": "Some object name",
      "value": 90
    }
  ],
  [  
    {  
      "id": "14",
      "name": "Third one",
      "value": 10
    }
  ]
]

function removeDuplicatesFromArray(arr){

 var obj={};
 var uniqueArr=[];
 for(var i=0;i<arr.length;i++){ 
    if(!obj.hasOwnProperty(arr[i])){
        obj[arr[i]] = arr[i];
        uniqueArr.push(arr[i]);
    }
 }

return uniqueArr;

}
var newArr = removeDuplicatesFromArray(testArray);
console.log(newArr);
manikant gautam
  • 3,521
  • 1
  • 17
  • 27
  • The code won't work with this kind of data: https://jsfiddle.net/9xdfdnsy/ It returns a single array instead of two separate ones. – abpetkov Dec 26 '17 at 10:44
0

const data = [  
  [  
    {  
      "id": "65",
      "name": "Some object name",
      "value": 90
    },
    {  
      "id": "89",
      "name": "Second Item",
      "value": 20
    }
  ],
  [  
    {  
      "id": "89",
      "name": "Second Item",
      "value": 20
    },
    {  
      "id": "65",
      "name": "Some object name",
      "value": 90
    }
  ],
  [  
    {  
      "id": "14",
      "name": "Third one",
      "value": 10
    }
  ]
];

const temp = {};
const result = [];

data.forEach(itemArr => {
  const items = itemArr.filter(item => {
    const isUnique = temp[`${item.id}-${item.name}-${item.value}`] === undefined;
    
    temp[`${item.id}-${item.name}-${item.value}`] = true;
    return isUnique;
  });
  if (items.length !== 0)
    result.push(items);
});

console.log(result);
Shriharsha KL
  • 317
  • 1
  • 2
  • 11
  • The only reason I can't accept is if there's another array with different objects, but one of them is duplicated from another array of objects, then it will get removed. – abpetkov Dec 13 '17 at 10:14
  • I don't think so. May be I'm unclear about your problem. Anyways, @manikant gautam's solution works I guess. – Shriharsha KL Dec 13 '17 at 10:52
  • Well, I meant something like this: https://jsfiddle.net/efag61se/ The result should have 2 arrays with 2 objects and 1 array with 3 objects. – abpetkov Dec 26 '17 at 10:45