Javascript array contains any

JavaScript Array includes()

The includes() method returns true if an array contains a specified value.

The includes() method returns false if the value is not found.

The includes() method is case sensitive.

Syntax

Parameters

Parameter Description
element Required.
The value to search for.
start Optional.
Start position. Default is 0.

Return Value

Browser Support

includes() is an ECMAScript7 (ES7) feature.

ES7 (JavaScript 2016) is supported in all modern browsers:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

includes() is not supported in internet Explorer or Edge 13 (or earlier).

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Array.prototype.includes()

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

Try it

Syntax

includes(searchElement) includes(searchElement, fromIndex) 

Parameters

Zero-based index at which to start searching, converted to an integer.

Return value

A boolean value which is true if the value searchElement is found within the array (or the part of the array indicated by the index fromIndex , if specified).

Description

The includes() method compares searchElement to elements of the array using the SameValueZero algorithm. Values of zero are all considered to be equal, regardless of sign. (That is, -0 is equal to 0 ), but false is not considered to be the same as 0 . NaN can be correctly searched for.

When used on sparse arrays, the includes() method iterates empty slots as if they have the value undefined .

The includes() method is generic. It only expects the this value to have a length property and integer-keyed properties.

Examples

Using includes()

[1, 2, 3].includes(2); // true [1, 2, 3].includes(4); // false [1, 2, 3].includes(3, 3); // false [1, 2, 3].includes(3, -1); // true [1, 2, NaN].includes(NaN); // true ["1", "2", "3"].includes(3); // false 

fromIndex is greater than or equal to the array length

If fromIndex is greater than or equal to the length of the array, false is returned. The array will not be searched.

const arr = ["a", "b", "c"]; arr.includes("c", 3); // false arr.includes("c", 100); // false 

Computed index is less than 0

If fromIndex is negative, the computed index is calculated to be used as a position in the array at which to begin searching for searchElement . If the computed index is less than or equal to 0 , the entire array will be searched.

// array length is 3 // fromIndex is -100 // computed index is 3 + (-100) = -97 const arr = ["a", "b", "c"]; arr.includes("a", -100); // true arr.includes("b", -100); // true arr.includes("c", -100); // true arr.includes("a", -2); // false 

Using includes() on sparse arrays

You can search for undefined in a sparse array and get true .

.log([1, , 3].includes(undefined)); // true 

Calling includes() on non-array objects

The includes() method reads the length property of this and then accesses each property whose key is a nonnegative integer less than length .

const arrayLike =  length: 3, 0: 2, 1: 3, 2: 4, 3: 1, // ignored by includes() since length is 3 >; console.log(Array.prototype.includes.call(arrayLike, 2)); // true console.log(Array.prototype.includes.call(arrayLike, 1)); // false 

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Jun 27, 2023 by MDN contributors.

Your blueprint for a better internet.

Источник

Javascript array contains any

Last updated: Dec 21, 2022
Reading time · 3 min

banner

# Table of Contents

# Check if Array contains any element of another Array in JS

To check if an array contains any element of another array:

  1. Use the Array.some() method to iterate over the first array.
  2. Check if each element is contained in the second array.
  3. If there is at least 1 common element, the Array.some() method will return true .
Copied!
const arr1 = ['pizza', 'cake', 'cola']; const arr2 = ['pizza', 'beer']; const contains = arr1.some(element => return arr2.includes(element); >); console.log(contains); // 👉️ true

The function we passed to the Array.some() method gets called with each element of the array.

If at least one invocation of the callback function returns a truthy value, the some() method returns true , otherwise, false is returned.

Copied!
const arr1 = ['pizza', 'cake', 'cola']; console.log(arr1.includes('pizza')); // 👉️ true console.log(arr1.includes('another')); // 👉️ false

The arrays in the example have a common element, so the Array.includes() method returns true , causing the Array.some() method to also return true .

Alternatively, you can use the indexOf() method.

# Check if Array contains any element of another Array using indexOf()

This is a three-step process:

  1. Use the Array.some() method to iterate over the first array.
  2. Use the Array.indexOf() method to check if each element is contained in the second array.
  3. If the Array.indexOf() method returns a value other than -1 , the arrays contain common elements.
Copied!
const arr1 = ['pizza', 'cake', 'cola']; const arr2 = ['pizza', 'beer']; const contains = arr1.some(element => return arr2.indexOf(element) !== -1; >); console.log(contains); // 👉️ true

If the arrays have a common element, then Array.indexOf will return the element’s index, otherwise, it returns -1 .

Copied!
const arr1 = ['pizza', 'cake', 'cola']; console.log(arr1.indexOf('pizza')); // 👉️ 0 console.log(arr1.indexOf('another')); // 👉️ -1

In the code example, there is a common element between the arrays, so the callback function returns true and the Array.some() method also returns true .

Alternatively, you can use a for. of loop.

# Check if Array contains any element of another Array using for. of

This is a three-step process:

  1. Declare a new variable and initialize it to false .
  2. Use a for. of loop to iterate over the first array.
  3. If any element is contained in the second array, set the variable to true .
Copied!
const arr1 = ['pizza', 'cake', 'cola']; const arr2 = ['pizza', 'beer']; let containsAny = false; for (const element of arr1) if (arr2.includes(element)) containsAny = true; break; > > console.log(containsAny); // 👉️ true

Notice that we declared the containsAny variable using let .

This is necessary because variables declared using const cannot be reassigned.

The for. of statement is used to loop over iterable objects like arrays, strings, Map , Set and NodeList objects and generators .

On each iteration, we check if the second array contains the current element.

If the condition is met, we know there is at least 1 common element, so we set the containsAny variable to true and exit the for. of loop.

The break statement terminates the current loop or switch statement.

# Check if Array contains any element of another Array using for

You can also use a basic for loop to check if an array contains any element of another array.

Copied!
const arr1 = ['pizza', 'cake', 'cola']; const arr2 = ['pizza', 'beer']; let containsAny = false; for (let index = 0; index arr1.length; index++) if (arr2.includes(arr1[index])) containsAny = true; break; > > console.log(containsAny); // 👉️ true

We initialized a boolean variable to false , just like in the previous code sample.

On each iteration, we check if the current element is contained in the second array.

If the condition is met, the arrays contain at least one common element, so we break out of the loop.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Читайте также:  Плавное появление элементов
Оцените статью