Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Check if an array contains a string in javascript

const colors = ['red', 'green', 'blue'];
const result = colors.includes('red');

console.log(result); // true
Comment

if array includes string

function droids(arr) {
  let result = 'These are not the droids you're looking for';
  for(let i=0; i<arr.length;i++) {
      if (arr[i] === 'Droids') {
      result = 'Found Droid';
    }
  }
  return result;
}

// Uncomment these to check your work! 
const starWars = ["Luke", "Finn", "Rey", "Kylo", "Droids"]
const thrones = ["Jon", "Danny", "Tyrion", "The Mountain", "Cersei"]
console.log(droids(starWars)) // should log: "Found Droids!"
console.log(droids(thrones)) // should log: "These are not the droi





//A simpler approach 

console.log(starWars.includes('Droids') ? 'Droid Found' : 'These are not the droids you're looking for');
console.log(thrones.includes('Droids') ? 'Droid Found' : 'These are not the droids you're looking for');
Comment

if Array includes string

function droids(arr) {
    result = "These are not the droids you're looking for." 
    
    for (str of arr) {
        if (str == 'Droids')
            result = "Found Droids!"
    }
    
    return result;
}
Comment

check if string contains a value in array

$count = 0;
str_replace($owned_urls, '', $string, $count);
// if replace is successful means the array value is present(Match Found).
if ($count > 0) {
  echo "One of Array value is present in the string.";
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: change string with string js 
Javascript :: js upload file size limit 
Javascript :: self invoking function in javascript 
Javascript :: how to add function in javascript 
Javascript :: call node.js file electron 
Javascript :: How to Define a Function using a Function Expression in javascript 
Javascript :: react custom hooks 
Javascript :: js date toisostring with timezone 
Javascript :: javascript function with input value 
Javascript :: find the second largest number in array javascript 
Javascript :: how to add data modal target attribute in jquery 
Javascript :: javascript onsubmit change input value 
Javascript :: how to see javascript code in chrome 
Javascript :: got back to start of for loop js 
Javascript :: set twig variable from javascript 
Javascript :: Beep sound Javascript 
Javascript :: join string js with and at the last item 
Javascript :: add text to innerhtml javascript 
Javascript :: playSound in draw loop javascript 
Javascript :: jquery-3.5.1.min.js download 
Python :: colab mount drive 
Python :: uuid regex 
Python :: how to install OpenCV? 
Python :: make jupyter notebook wider 
Python :: drop a column pandas 
Python :: column dataframe to int 
Python :: enumerate zip python 
Python :: how to add percentage in pie chart in python 
Python :: conda install dash 
Python :: EnvironmentError command line 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =