ArrayContains

Returns the position of the first element in the array that matches the item that we are searching for, or 0 if the item is not found.

ArrayContains( haystack, needle [, substringMatch ] )

Returns: Number

Argument Description
haystack
array, required

The array in which to search for the item

Alias: array, arr

needle
object, required

The item that we are looking for in the array

Alias: object, obj, o

substringMatch
boolean, optional

If set to true then a substring match will also return an array position. This will only work with simple values. Passing true with complex objects will throw an exception.

Examples

numbers         = [ 1, 2, 3, 4 ];
positionOfThree = ArrayContains( numbers, 3 );
Echo( positionOfThree ); // outputs 3
words               = [ 'hello' , 'world' ];
positionOfWorld     = ArrayContains( words, 'world' );
positionOfSubstring = ArrayContains( words, 'el', true );
Echo( positionOfSubstring ); // outputs 1
Echo( positionOfWorld     ); // outputs 2

See also