Scholar.inArray(needle, haystack) and Scholar.arraySearch(needle, haystack) -- versions of the PHP functions for JS

This commit is contained in:
Dan Stillman 2006-06-20 17:32:40 +00:00
parent 6b002f7566
commit 6c89acbe0d

View file

@ -23,6 +23,8 @@ var Scholar = new function(){
this.getString = getString;
this.flattenArguments = flattenArguments;
this.join = join;
this.inArray = inArray;
this.arraySearch = arraySearch;
this.randomString = randomString;
this.getRandomID = getRandomID;
@ -198,6 +200,34 @@ var Scholar = new function(){
}
/*
* PHP's in_array() for JS -- returns true if a value is contained in
* an array, false otherwise
*/
function inArray(needle, haystack){
for (var i in haystack){
if (haystack[i]==needle){
return true;
}
}
return false;
}
/*
* PHP's array_search() for JS -- searches an array for a value and
* returns the key if found, false otherwise
*/
function arraySearch(needle, haystack){
for (var i in haystack){
if (haystack[i]==needle){
return i;
}
}
return false;
}
/**
* Generate a random string of length 'len' (defaults to 8)
**/