慕姐8265434
2022-12-22 13:47:27
我有一個包含的數(shù)組const submissions = [{ question: 'blah blah blah blah', response: 'response goes here'},我有一個接受提交數(shù)組和字符串的函數(shù)。我需要檢查響應(yīng)提交 [i].response 對字符串。如果存在則返回 true。let submissions = [{ question: 'blah blah blah blah', response: 'response goes here'}];function checkString(submissions, string) { for (let i = 0; i < submissions.length; i++) { if (submissions[i].response === string) { return true; } } return false;}console.log(checkString(submissions,'response goes here'));如果調(diào)用函數(shù) checkString(submissions, 'response goes here') 應(yīng)該返回 true。這總是返回 false。
2 回答

嗶嗶one
TA貢獻(xiàn)1854條經(jīng)驗 獲得超8個贊
您可以簡單地使用一些函數(shù)并包含函數(shù),該函數(shù)將檢查是否array
response
包含string
您正在檢查的內(nèi)容。
該
includes()
方法確定數(shù)組是否在其條目中包含某個值,返回true
或false
適當(dāng)。
let submissions = [{
question: 'blah blah blah blah',
response: 'response goes here'
}];
var checkStr = submissions.some(i => i.response.includes('response goes here'));
console.log(checkStr)
添加回答
舉報
0/150
提交
取消