How to find element of array that is string contains a value in array in javascript

11/28/2022

#element of array that is string contains a value in array in javascript #javascript #coding #program in javascript

Go Back

How to find element of array that is  string contains a value in array in javascript

Here we see Example of  finding array element in String .So basically there is two method to find  Array value in String 

How to Find an Array Element in a String in JavaScript

Introduction

Finding whether a string contains a value from an array is a common task in JavaScript. There are multiple ways to check if an array contains a specific value or if any element in an array matches a substring in a given string.

In this guide, we will explore two approaches:

  1. Checking if an array contains an exact value.
  2. Checking if a string contains any value from an array.
#element of array that is  string contains a value in array in javascript #javascript  #coding #program in javascript

Method 1: Checking if an Array Contains a Specific Value

JavaScript provides the indexOf() method to check if a value exists in an array. This method returns the index of the value if found, otherwise it returns -1.

Example:

var myarr = ['java', 'python', 'scala', 'nodejs', 'bigdata', 'hive', 'sql', 'HTML'];
var arrayContainsValue = (myarr.indexOf("turtles") > -1);

console.log(arrayContainsValue); // Output: false

Explanation:

  • The indexOf() method searches for the element "turtles" in the array myarr.
  • If the value is found, it returns the index (0 or higher), indicating the position of the element.
  • If the value is not found, it returns -1, meaning the element is not present in the array.

Method 2: Finding a Value from an Array in a Given String

If we need to check whether a string contains any value from an array, we can use the some() method along with the includes() method.

Example:

function getKeyWord() {
    var language = "null";
    const str = "I am a learner of HTML";
    const arr = ['java', 'python', 'scala', 'nodejs', 'bigdata', 'hive', 'sql', 'HTML'];

    const contains = arr.some(element => {
        if (str.includes(element)) {
            language = element;
            return true;
        }
        return false;
    });

    return language;
}

console.log(getKeyWord()); // Output: "HTML"

Explanation:

  • The function getKeyWord() initializes a language variable to "null".
  • The some() method iterates over the array and checks if str.includes(element).
  • If a match is found, language is updated with the matching element.
  • The function returns the found keyword, or "null" if no match is found.

Key Takeaways:

  • Use indexOf() to check for an exact value in an array.
  • Use some() with includes() to check if a string contains any value from an array.
  • These methods are useful in search filtering, keyword detection, and data validation tasks.

Conclusion

JavaScript provides simple yet effective methods to check for values in arrays and strings. Depending on the use case, you can either check for an exact match in an array using indexOf() or find a substring match in a string using some() with includes().

By implementing these methods, developers can efficiently search and manipulate string and array data in JavaScript applications.

Table of content