Created
December 4, 2018 14:13
-
-
Save hassansw/f2297fc2b3c7787a9a905e77a9f0eeab to your computer and use it in GitHub Desktop.
Find Duplicate objects in an Array of Objects in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkDuplicateInObject(propertyName, inputArray) { | |
let seenDuplicate = false, testObject = {}; | |
let dupItem = null | |
inputArray.map((item) => { | |
const itemPropertyName = item[propertyName]; | |
if (itemPropertyName in testObject) { | |
testObject[itemPropertyName].duplicate = true; | |
item.duplicate = true; | |
seenDuplicate = true; | |
dupItem = item | |
} | |
else { | |
testObject[itemPropertyName] = item; | |
delete item.duplicate; | |
} | |
}); | |
return {res:seenDuplicate, payload: dupItem}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment