Go Back

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

11/29/2022
All Articles

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

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

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 

In below first ,we can find exact value of element in Array .

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

Note: In above function inArray returns the index of the element that is found, so 0 indicates the element is the first in the array. -1 indicates the element was not found.

The second example is find value of array in given  String so if we able to match value in string

So it return that value :

 getKeyWord is function that use to return value  that is match in given string as follow :

function getKeyWord(){
var language="null" ;
const str = "I am 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 language;
  }
  else
  return false;
});
return language;
 
}
console.log(""+getKeyWord())

Article