Skip to content

Instantly share code, notes, and snippets.

@jtarleton
Created January 29, 2013 17:26
Show Gist options
  • Save jtarleton/4665966 to your computer and use it in GitHub Desktop.
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.
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