Created
July 5, 2018 20:58
-
-
Save SirSerje/241a9b8808ada048b445f5c43a3d41b4 to your computer and use it in GitHub Desktop.
How to filter array with similar object (small check of deep compare)
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 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