Last active
November 28, 2024 08:18
-
-
Save frontenddeveloping/9893164 to your computer and use it in GitHub Desktop.
Array.prototype.mergeAsNumber example
This file contains hidden or 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
Array.prototype.mergeAsNumber = function (array) { | |
var sortFunction = function (a,b) {return a-b}, | |
commonArray = this.concat(array).sort(sortFunction), | |
element, | |
suchNumberAnotherIndex; | |
for (var i = 0, l = commonArray.length; i<l; i++) { | |
element = commonArray[i]; | |
suchNumberAnotherIndex = commonArray.indexOf(element, i+1); | |
while (suchNumberAnotherIndex > -1) { | |
delete commonArray[suchNumberAnotherIndex]; | |
i++; | |
suchNumberAnotherIndex = commonArray.indexOf(element, i); | |
} | |
} | |
return commonArray.sort(sortFunction); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment