2 回答

TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊
您可以調(diào)用some
該數(shù)組并返回 的結(jié)果theString.startsWith(theArrayEntryForEachLoopIteration)
,如下所示:
const?theString?=?"Applepie"; const?result?=?fruits.some(fruit?=>?theString.startsWith(fruit));
result
如果true
存在匹配(您的回調(diào)返回真值),則為,false
如果不存在(您的回調(diào)從未返回真值)。some
當(dāng)回調(diào)第一次返回真值時(shí),也會(huì)停止查找,因?yàn)闆](méi)有必要繼續(xù)查找。
實(shí)例:
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貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊
const text = 'Applepie'; //Input Text
const fruits = ['Apple', 'Banana', 'Orange'];
console.log(fruits.some(v => text.startsWith(v)));
添加回答
舉報(bào)