Created
January 29, 2013 17:26
-
-
Save jtarleton/4665966 to your computer and use it in GitHub Desktop.
Two high-level functions, "array_search" and "array_unique" for good old Javascript.
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
function arrayUnique(ar) | |
{ | |
if(ar.length && typeof ar!=='string') | |
{ | |
var sorter = {}; | |
var out = []; | |
for(var i=0,j=ar.length;i<j;i++) | |
{ | |
if(!sorter[ar[i]+typeof ar[i]]) | |
{ | |
out.push(ar[i]); | |
sorter[ar[i]+typeof ar[i]]=true; | |
} | |
} | |
} | |
return out || ar; | |
} | |
function arraySearch(arr,val) { | |
for (var i=0; i<arr.length; i++) | |
if (arr[i] == val) | |
return i; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment