“Periksa apakah array berisi string di JavaScript” Kode Jawaban

JS Periksa apakah string termasuk dari array

wordsArray = ['hello', 'to', 'nice', 'day']
yourString = 'Hello. Today is a nice day'.toLowerCase()
result = wordsArray.every(w => yourString.includes(w))
console.log('result:', result)
Borma

Periksa apakah array berisi string di JavaScript

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

console.log(result); // true
Outrageous Ostrich

Periksa apakah array tidak berisi string js

function checkInput(input, words) {
 return words.some(word => new RegExp(word, "i").test(input));
}

console.log(checkInput('"Definitely," he said in a matter-of-fact tone.', 
["matter", "definitely"]));
Vu Nguyen

Jika array termasuk 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');
Energetic Eel

Jawaban yang mirip dengan “Periksa apakah array berisi string di JavaScript”

Pertanyaan yang mirip dengan “Periksa apakah array berisi string di JavaScript”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya