2 回答
TA贡献1813条经验 获得超2个赞
您可以调用some
该数组并返回 的结果theString.startsWith(theArrayEntryForEachLoopIteration)
,如下所示:
const theString = "Applepie"; const result = fruits.some(fruit => theString.startsWith(fruit));
result
如果true
存在匹配(您的回调返回真值),则为,false
如果不存在(您的回调从未返回真值)。some
当回调第一次返回真值时,也会停止查找,因为没有必要继续查找。
实例:
const fruits = ['Apple', 'Banana', 'Orange'];
// Example where it's there:
const theString = "Applepie";
const result = fruits.some(fruit => theString.startsWith(fruit));
console.log(result);
// Example where it isn't
const theString2 = "Toffeepie";
const result2 = fruits.some(fruit => theString2.startsWith(fruit));
console.log(result2);
TA贡献1816条经验 获得超4个赞
const text = 'Applepie'; //Input Text
const fruits = ['Apple', 'Banana', 'Orange'];
console.log(fruits.some(v => text.startsWith(v)));
添加回答
举报