var sentence = 'The quick brown fox jumped over the lazy dog.';
var word = ['fox'];
console.log('The word "' + word + (sentence.includes(word)? '" is' : '" is not') + ' in the sentence');
// expected output: "The word "fox" is in the sentence"
Это String.includes()
в документах Javascript, но то, что я хочу сделать, выглядит примерно так:
var sentence = 'The quick brown fox jumped over the lazy dog.';
var word = ['fox', 'dog'];
console.log('The word "' + word + (sentence.includes(word)? '" is' : '" is not') + ' in the sentence');
Я хочу проверить массив строк на предложение и вернуть true
если одна из строк в массиве находится в предложении. Но, как вы можете видеть во фрагменте, это не работает.
попробуйте это вместо этого, если одно из var word
не совпадает с предложением, оно вернет false else, если все включено, тогда оно вернет true.
var sentence = 'The quick brown fox jumped over the lazy dog.'
var word = ["fox", "the", "lol"]
var initialReturn = true
check = (word) => {
!sentence.includes(word) ? initialReturn = false : null
}
word.map(item => {
return check(item)
})
console.log("initialReturn", initialReturn)
Я предполагаю, что он печатает для каждого элемента в переменном word
So like
"The word "fox" is in the sentence"
"The word "dog" is not in the sentence"
Ты можешь сделать
var sentence = 'The quick brown fox jumped over the lazy dog.';
var word = ["fox", "dog"]
function test(word) {
console.log('The word "' + word + (sentence.includes(word) ? '" is' : '" is not') + ' in the sentence');
}
word.map((item) => {
return test(item)
})
Проверьте этот фрагмент кода. Я изучил одну важную вещь, которая работает со струнами. Регулярные выражения могут помочь вам (но, черт возьми) и использовать расширения (функции, помощники) в вашей структуре, вы используете его. Но если вы используете native js, то indexOf поможет вам. Я немного обновил свой ответ.
var sentence = 'The quick brown fox jumped over the lazy dog.';
var words = ['fox', 'dog'];
console.log('The words: "' + words + (sentence.split(/[ ,.]/).some(function(e) { return words.indexOf(e) >= 0 })? '" is' : '" is not') + ' in the sentence');
var sentence = 'The quick brown fox jumped over the lazy dog.';
var word = ['fox', 'dog'];
console.log('The word "' + word + (sentence.split(' ').some(d => word.includes(d)) ? '" is' : '" is not') + ' in the sentence');
some
слова вместо