AppDividend
Latest Code Tutorials

Javascript Array Includes Example | Array.prototype.includes() Tutorial

0

Javascript Array Includes Example | Array.prototype.includes() Tutorial is today’s topic. The includes() method determines whether the array contains the specified element. It returns true or false as output depending on the result. The Javascript Array includes() method is case sensitive. In this post, we will see array contains an object, the array includes string, the array contains a substring.

Javascript Array Includes Example

The syntax of Javascript Array Includes is following.

array.includes(element, start)

The element parameter is required, and it is the element which we need to search.

The start parameter is an optional parameter, and the default is 0. It is the position in the array to start the search.

Let’s take an example of Javascript Array Includes.

// app.js

let arr = ['Gavin', 'Richard', 'Erlich', 'Gilfoyle'];

console.log(arr.includes('Erlich'));

As we know, Erlich is in the arr. So it will return true.

Javascript Array Includes Example

If we search for an element which does not exist in an array, then it will return the false.

// app.js

let arr = ['Gavin', 'Richard', 'Erlich', 'Gilfoyle'];

console.log(arr.includes('Jared'));

The output is following.

Array.prototype.includes() Tutorial

When comparing strings and characters, includes() function is case-sensitive.

The includes() uses the sameValueZero algorithm to determine whether the given element is found.

Let’s see other examples.

// app.js

console.log([1, 2, 3].includes(3, 2));
console.log([19, 21, 46].includes(19, 1));
console.log([19, 21, 46].includes(46, -1));
console.log([1, 2, NaN].includes(NaN));

The output of the above code is following.

Javascript Array Includes Example | Array.prototype.includes() Tutorial

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

If the start position is negative, the computed index is calculated to be used as a position in an array at which to begin searching for the element. If the computed index is less or equal than -1 * array.length, an entire array will be searched.

#Javascript array contains

As we know until now, JS Array includes() method determines whether the array contains an item or specified an element or not. We can also check if an array includes an object in JavaScript.

#Array includes an object

Here, we can use the JS Array filter() method. See the following example.

// app.js
let list = [{name: 'Krunal'}, {name: 'Ankit'}];

let op = list.filter(data => (data.name == 'Krunal'));

console.log(op)

See the output.

Array includes an object

We can also use the instanceOf operator to check the type of the element. Combination of array filter and instanceOf operator, we can check whether the array contains an object or array includes the object. 

See the following code example.

// app.js
let obj = { author: 'KDL' }
let list = [obj, obj];

let op = list.filter(data => data instanceof Object);
console.log(op)

See the following output.

array contains object

So, from the output, we can say the array contains object otherwise filter method filter out the elements and does not include in the final output.

#Array contains a specific String in JavaScript

If we want to find whether an array contains a string then we can use array indexOf method in Javascript. See the following programming example.

// app.js
let arr = ['Krunal', 'Ankit', 'Rushabh'];
let arrContainString = (arr.indexOf('Krunal') > -1);
console.log(arrContainString);

Now, see the output.

Array contains a specific String

That means a specific string includes in Javascript array.

We can also verify using instanceOf operator.

#Array Contains String Constructor

See the following code.

// app.js

let strA = new String('Krunal');
let strB = new String('Ankit');
let strC = new String('Rushabh');

let arr = [strA, strB, strC];

let arrContainString = arr.filter(element => element instanceof String);
console.log(arrContainString);

See the output.

Array Contains String Constructor

#Array contains substring

In javascript, here is how you can search an array for a substring match.

// app.js

 let arr = ['Krunal', 'Ankit', 'Rushabh']
let index, value, result;
for (index = 0; index < arr.length; ++index) {
    value = arr[index];
    if (value.substring(0, 3) === "Ank") {
      result = value;
        break;
    }
}
if (result) {
  console.log(result)
}
else {
  console.log('Oops!! Not found')
}

See the output.

Array contains substring

In the above code, we have used the for loop and String substring method to check the substring in the array.

#Javascript array contains another array

If we want to determine whether one array contains another array in Javascript then we need to use the combination of the array every() and array indexOf() methods. See the code.

// app.js

let arr = [19, 21, 29, 46];
let arrB = [2, 6, 18, 19, 29, 46, 53, 21];

let op = arr.every(element => arrB.indexOf(element) > -1);
console.log(op);

See the output.

Javascript array contains another array

In the above code, the second array arrB contains all the elements of the arr that is why every function returns true.

If one element is missing from arrB then it will output in false. So keep in mind that above way only works if you need to check the whole array and not some items of the array.

Finally, Javascript Array Includes Example | Array.prototype.includes() Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.