Skip to content

Instantly share code, notes, and snippets.

@SirSerje
Created July 5, 2018 20:58
Show Gist options
  • Save SirSerje/241a9b8808ada048b445f5c43a3d41b4 to your computer and use it in GitHub Desktop.
Save SirSerje/241a9b8808ada048b445f5c43a3d41b4 to your computer and use it in GitHub Desktop.
How to filter array with similar object (small check of deep compare)
function getUniqueObject(nonUniqueArray) {
let result = [];
for (let i = 0; i < nonUniqueArray.length; i++) {
let currentItem = nonUniqueArray[i];
if (isObjectPresent(result, currentItem)) {
console.log('object is similar')
} else {
result.push(currentItem)
}
}
return result
}
function isObjectPresent(incomeArray, objectToVerify) {
for (let i = 0; i < incomeArray.length; i++) {
let currentItem = incomeArray[i];
if (isSimilar(currentItem, objectToVerify)) return true
}
return false
}
function isSimilar(object1, object2) {
if (object1.m === object2.m && object1.y === object2.y) return true
return false
}
console.log(getUniqueObject([{y: 6, m: 2018}, {y: 6, m: 2018}, {y: 2, m: 2018}, {y: 6, m: 2012}, {y: 6, m: 2018}]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment