gpt4 book ai didi

javascript - 字符串包括 : test the word from an array of strings

转载 作者:行者123 更新时间:2023-11-30 11:13:05 25 4
gpt4 key购买 nike

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"

这是 Javascript 文档中的 String.includes() 但我想做的是这样的:

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。但是正如您在代码片段中看到的那样,它不起作用。

最佳答案

我假设它为变量 word 中的每个项目打印好喜欢

 "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)
})

关于javascript - 字符串包括 : test the word from an array of strings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52849883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com