Check an Array Containing Specific Item in JavaScript.

0

In this blog post you’ll learn how to check an array containing a specific item or not in JavaScript. We’ll see different approaches to check whether the item is present in the array or not.


JavaScript Method Used:

includes()
indexOf()
filter()
find()
findindex()


JavaScript loops used:

For loop.


Check If Element or Item Present in Array or Not.

Approach 1: Using Includes() Method.

Includes methods return the true or false. If an element is present in an array it returns true otherwise it returns false. The method is case sensitive.


JavaScript

    let text_array = ["hello", "welcome", "to", "maketechstuff.com"];

    document.write(text_array.includes("hello"));
  


JavaScript


    let text_array = ["hello", "welcome", "to", "maketechstuff.com"];

    let search_item = "hello";
    let includes_value = text_array.includes(search_item)
    if (includes_value === true) {
        document.write("text_array contains the item " + search_item);
    } else {
        document.write("text_array <b>does not</b> contains the item " + search_item);
    }
    // Output:
    // text_array contains the item hello



Approach 2: Using IndexOf() Method.

IndexOf method returns the index of a given element. If an element is not present in the array then it returns -1. The method is case sensitive.


JavaScript


    let text_array = ["hello", "welcome", "to", "maketechstuff.com"];

    let index_id = text_array.indexOf("hello");



JavaScript


    let text_array = ["hello", "welcome", "to", "maketechstuff.com"];

    let search_item = "Hello";
    let index_id = text_array.indexOf(search_item);
    if (index_id !== -1) {
        document.write("text_array contains the item " + search_item);
    } else {
        document.write("text_array <b>does not</b> contains the item " + search_item);
    }
    // Output:
    // text_array does not contains the item Hello


indexOf method is case sensitive.


Approach 3: Using for loop.

In this approach we check individual elements of an array with the value we've been searching for.


JavaScript


    let text_array = ["hello", "welcome", "to", "maketechstuff.com"];

    let search_item = "maketechstuff.com";
    for (let i = 0; i < text_array.length; i++) {
        const element = text_array[i];
        if (element === search_item) {
            document.write("Index value: ", i);
            document.write("<br>");
            document.write("text_array contains the item ", search_item);
        }
    }
    // Output:
    // Index value: 3
    // text_array contains the item maketechstuff.com

  


The above example is case sensitive. To make it case insensitive we first change the text case and then check for match of search value with individual array items.


JavaScript


    let search_item = "Maketechstuff.com";
    for (let i = 0; i < text_array.length; i++) {
        const element = text_array[i];
        if (element.toLowerCase() === search_item.toLowerCase()) {
            document.write("Index value: ", i);
            document.write("<br>");
            document.write("text_array contains the item ", search_item);
        }
    }
    // Output:
    // Index value: 3
    // text_array contains the item Maketechstuff.com



Approach 4: Using filter() Method.

JavaScript


    let text_array = ["hello", "welcome", "to", "maketechstuff.com"];

    let search_item = "hello";
    let result = text_array.filter(function (item, index) { return item == search_item });

    if (result.length !== 0) {
        document.write("text_array contains the item ", search_item);
    } else {
        document.write("text_array <b>does not</b> contains the item ", search_item);

    }
    // Output:
    // text_array contains the item hello


  


Approach 5: Using find() Method.

Find method returns the first element that passes the given text or condition. If no element is found then it returns undefined.


JavaScript


    let text_array = ["hello", "welcome", "to", "maketechstuff.com"];

    let search_item = "Hello";
    let result = text_array.find(function (v, i, a) { return v === search_item })
    console.log(result);
    if (result !== undefined) {
        document.write("text_array contains the item ", search_item);
    } else {
        document.write("text_array <b>does not</b> contains the item ", search_item);
    }
    // Output:
    // text_array does not contains the item Hello




Approach 6: Using findIndex() Method.

Findindex method returns the index value of the first match element and returns -1 if no element is found.


JavaScript


    let text_array = ["hello", "welcome", "to", "maketechstuff.com"];

    let search_item = "hello";
    let result = text_array.findIndex(function (item) { return item === search_item })
    console.log(result);
    if (result !== -1) {
        document.write("text_array contains the item ", search_item);
    } else {
        document.write("text_array <b>does not</b> contains the item ", search_item);
    }
    // Output:
    // text_array contains the item hello


So that's how you can check if the specific or given item is present inside the array or not in JavaScript. If you have any query or suggestion feel free to write in the comment section.


Tags

Post a Comment

0Comments

Share your thoughts.

Post a Comment (0)
To Top