Techniques for Removing Duplicate Objects from JavaScript Arrays
Consider an array of objects where duplicates need to be eliminated based on a specific property:
const dataSet = [
{ id: '01', name: 'Lele' },
{ id: '02', name: 'Bobo' },
{ id: '03', name: 'Taotao' },
{ id: '04', name: 'Haha' },
{ id: '01', name: 'Lele' }
];
Method 1: Using an Object Lookup
This approach employs an object as a lookup table to track encountered keys.
function removeDuplicatesByProperty(arr, property) {
const uniqueItems = [];
const seenKeys = {};
for (let i = 0; i < arr.length; i++) {
const currentKey = arr[i][property];
if (!seenKeys[currentKey]) {
uniqueItems.push(arr[i]);
seenKeys[currentKey] = true;
}
}
return uniqueItems;
}
const deduped = removeDuplicatesByProperty(dataSet, 'id');
console.log(deduped);
// Output: [{id: "01", name: "Lele"}, {id: "02", name: "Bobo"}, {id: "03", name: "Taotao"}, {id: "04", name: "Haha"}]
Method 2: Utilizing the Reduce Method
The reduce method accumulates unique items while checking against a tracking object.
function deduplicateWithReduce(array, keyField) {
const keyTracker = {};
return array.reduce((accumulator, currentElement) => {
const identifier = currentElement[keyField];
if (!keyTracker[identifier]) {
keyTracker[identifier] = true;
accumulator.push(currentElement);
}
return accumulator;
}, []);
}
const result = deduplicateWithReduce(dataSet, 'id');
console.log(result);
// Output: [{id: "01", name: "Lele"}, {id: "02", name: "Bobo"}, {id: "03", name: "Taotao"}, {id: "04", name: "Haha"}]
Method 3: Comprehensive Object Comparison
For scenarios requiring deduplication based on entire object content rather than a single property, this method serializes objects for cmoparison.
function eliminateObjectDuplicates(objectArray) {
const uniqueObjects = [];
const serializedSet = {};
for (let i = 0; i < objectArray.length; i++) {
const currentObj = objectArray[i];
const propertyNames = Object.keys(currentObj).sort();
let serializedString = '';
for (let j = 0; j < propertyNames.length; j++) {
const propName = propertyNames[j];
serializedString += JSON.stringify(propName) + JSON.stringify(currentObj[propName]);
}
if (!serializedSet.hasOwnProperty(serializedString)) {
uniqueObjects.push(currentObj);
serializedSet[serializedString] = true;
}
}
return uniqueObjects;
}
const complexData = [
{ id: '01', value: 'Test', tag: 'A' },
{ tag: 'A', value: 'Test', id: '01' },
{ id: '02', value: 'Demo' }
];
const filtered = eliminateObjectDuplicates(complexData);
console.log(filtered);
// Output: [{id: "01", value: "Test", tag: "A"}, {id: "02", value: "Demo"}]