Created
June 8, 2018 10:11
-
-
Save msaxena25/a8b34d644167b7e40f57474c25f9483b to your computer and use it in GitHub Desktop.
Array (contains Object type element) get Unique objects items only
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
1. Compare two arrays contains Object type element | |
2. Below we are checking based on one property "id" | |
3. remove duplicate items | |
(<any>Array.prototype).unique = function () { | |
var a = this.concat(); | |
for (var i = 0; i < a.length; ++i) { | |
for (var j = i + 1; j < a.length; ++j) { | |
if (a[i].id === a[j].id) // checking based on id property | |
a.splice(j--, 1); | |
} | |
} | |
return a; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment