Remove duplicated elements on an array of objects can become complicated specially when having nested attributes or fields, we would need to do a deep compare to truly know if the element is repeated or not
To solve this issue we are going to use and npm module called just-diff this package will help us compare two objects and get the differences between both, this packages does a deep comparison, so it also works with nested attributes
First we are going to install the npm module using the following command
npm install just-diff
Now we are going to create a function that receives an array of objects and return an array without duplicates
import { diff } from "just-diff";
/**
* @desc Receives and array of objects and remove duplicate elements
* @param array array of objects
*/
export const removeArrayObjectsDuplicates = (array: any[]) => {
// Array without duplicates that we are going to return
const arrayWithoutDuplicates = [];
// Loop through each element of array
for (const element of array) {
let duplicateFound = false;
// Loop through each element on arrayWithoutDuplicates
for (const elementWithoutDuplicates of arrayWithoutDuplicates) {
const changes = diff(element, elementWithoutDuplicates);
// If changes were not found change duplicatedFound to true
if (changes.length === 0) {
duplicateFound = true;
break;
}
}
// If no duplicated were found add element to arrayWithoutDuplicates
if (!duplicateFound) {
arrayWithoutDuplicates.push(element);
}
}
return arrayWithoutDuplicates;
};
// Examples using the function above
const arrayWithDuplicates = [{ a: 1 }, { a: 1 }, { a: 15 }];
const secondArrayWithDuplicates = [{
a: {
b: 1,
c: 2
},
d: [1, 2, 3]
},
{
a: {
b: 1,
c: 2
},
d: [1, 2, 4]
}, {
a: {
b: 1,
c: 2
},
d: [1, 2, 3]
}, {
a: {
b: 4,
c: 6
},
d: [2, 3, 4]
}];
const firstResult = removeArrayObjectsDuplicates(arrayWithDuplicates);
// firstResult = [{"a":1},{"a":15}];
const secondResult = removeArrayObjectsDuplicates(secondArrayWithDuplicates);
// secondResult = [{"a":{"b":1,"c":2},"d":[1,2,3]},{"a":{"b":1,"c":2},"d":[1,2,4]},{"a":{"b":4,"c":6},"d":[2,3,4]}]