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 :: Cache and return requests 
Javascript :: jquery confirm dialog yes no 
Javascript :: how create a random enum on postman variable 
Javascript :: GetNameOfZone 
Javascript :: useLinkClickHandler 
Javascript :: Edit todo list react-redux 
Javascript :: how to get all words in a string that exists between two charachters using rejx js 
Javascript :: Arrow Function Shorthand javascript 
Javascript :: how to make console log hello in discord.js 
Javascript :: make a backend server in node 
Javascript :: object filter 
Javascript :: simple promise 
Javascript :: how to escape double quotes in json 
Javascript :: Moralis Password reset web3 
Javascript :: how to end tsc main --watch 
Javascript :: service erstellen angular 
Javascript :: vscode redirect back 
Javascript :: javascript const scope = await angular.element(document.body).scope(); 
Javascript :: combine strings in angularjs 
Javascript :: js dom after selectors 
Javascript :: recharts area chart 
Javascript :: typeorm caching queries time limit globally 
Javascript :: how to test conditional rendering vue test utils 
Javascript :: node "promise.all" "retry" site:stackoverflow.com 
Javascript :: Automatically Refresh or Reload a Page using http-equiv 
Javascript :: blur area behind an element in react native 
Javascript :: unique elements 
Javascript :: itreating string js 
Javascript :: keep form values after submit javascript 
Javascript :: const isEnabled = !Object.keys(errors).some(x = errors[x]); 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =