Jun
3
2011

Javascript equivalent of PHP’s in_array() function

The in_array() function of PHP is super helpful when you need to quickly check a variable against an array of possible options.

I am unsure why there is not a built in equivalent for javascript. That is one of many reasons why you may want to research mooTools or jQuery and start using it in your projects.


From MooTools:

Array method: contains

  • Tests an array for the presence of an item.

Syntax:

var inArray = myArray.contains(item[, from]);

Arguments:

  • item – (object) The item to search for in the array.
  • from – (number, optional: defaults to 0) The index of the array at which to begin the search.

Returns:

  • (boolean) If the array contains the item specified, returns true. Otherwise, returns false.

Examples:

['a', 'b', 'c'].contains('a'); // returns true
['a', 'b', 'c'].contains('d'); // returns false

From jQuery:

jQuery.inArray( value, array )

Arguments:

  • value The value to search for.
  • array An array through which to search.

The $.inArray() method is similar to JavaScript’s native .indexOf() method in that it returns -1 when it doesn’t find a match. If the first element within the array matches value, $.inArray() returns 0.

Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), if we’re checking for the presence of value within array, we need to check if it’s not equal to (or greater than) -1.

Example: Report the index of some elements in the array.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:blue; }
  span { color:red; }
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
<div>"John" found at <span></span></div>
<div>4 found at <span></span></div>
<div>"Karl" not found, so <span></span></div>
<script>var arr = [ 4, "Pete", 8, "John" ];

$("span:eq(0)").text(jQuery.inArray("John", arr));
$("span:eq(1)").text(jQuery.inArray(4, arr));
$("span:eq(2)").text(jQuery.inArray("Karl", arr));

</script>

</body>
</html>

Demo:

  • “John” found at 3
  • 4 found at 0
  • “Karl” not found, so -1

This simple example of just one utility built into both of these very popular libraries that illustrates why i much prefer mooTools over jQuery. If you read through the syntax and what is returned, it just makes more sense to me.

Ok with that said, if you still think you need a javascript only version of the PHP in_array function the below code will achieve what you are looking for:

Arguments:

  • item – The item in the array to search for

Returns:

  • (boolean) If the array contains the item specified, returns true. Otherwise, returns false.
Array.prototype.in_array = function(a) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}

Demo

var v_array = [1,2,3,4,5,6,7,8,9,'hello'];
document.writeln(v_array.in_array(1));  // true
document.writeln(v_array.in_array(11));  // false
document.writeln(v_array.in_array('hello'));  // true

Leave a comment

Show/Hide Footer Actions

Status: Available for your project.