array_search() in Javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    array_search() in Javascript?

    Is there any function in JS similar to array_search() of PHP which returns the key that contains the particular value, passed as argument. Or I need to make it myself?
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Well, I guess there's not!


    Code:
    function arraySearch(arr,val) {
    	for (var i=0; i<arr.length; i++)
    	if (arr[i] == val)
    	return i;
    	return false;
    }

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      If you want PHP equivalent functions, see this link. Your code is similar (though could do with some proper indentation).

      Comment

      Working...