AppDividend
Latest Code Tutorials

Javascript Array Includes Example | Javascript Array Contains

0

In this tutorial, we will see about Javascript array includes() function. 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, and the array includes string, the array contains a substring.

Javascript Array Includes Example

The syntax of Javascript Array Includes is the following.

array.includes(element, start)

The element parameter is required, and it is the element that 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 that 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'));

See the following output.

Array.prototype.includes() Tutorial

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

Javascript array includes() function 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 an object; otherwise, the filter() method filters out the elements and does not include in the final output.

#Javascript array contains a specific string

If we want to find whether an array contains a string, then we can use the 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.

#Javascript 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

#Javascript 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 is false. So keep in mind that the above way only works if you need to check the whole array and not some items of the array.

#Javascript array includes multiple conditions

If we have multiple conditions for JavaScript .includes() method, then we can create a custom function that checks if one, and only one of the conditions is true. See the following code.

// app.js

let str = "Friends Don't Lie";
let arr = ['Friends','Eleven', 'Millie']

const contains = (target, pattern) => {
    let value = 0;
    pattern.forEach(function(word){
      value = value + target.includes(word);
    });
    return (value === 1)
}

console.log(contains(str, arr));

So, in the above example, we have one string and one array.

Now, we take one by one array element and check inside the string using includes() method and if any element found in the string, then it returns true. We are checking each element using an array.forEach() function.

In our example, Friends is there in the string so that it will return true.

See the output.

➜  es git:(master) ✗ node app
true
➜  es git:(master) ✗

Finally, Javascript Array Includes Example | Javascript Array Contains Tutorial is over.

Recommended Posts

Javascript Array Concat Example

Javascript Array isArray Example

JavaScript Array Reverse Example

Javascript Array ToString Example

Javascript Array Splice Example

Javascript Array Slice Example

Javascript Array Join Example

Javascript Array Reduce Example

Javascript Array Map Example

Javascript Array Find Example

Leave A Reply

Your email address will not be published.

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