亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

比較 array[i].response 到通過函數參數傳遞的字符串

比較 array[i].response 到通過函數參數傳遞的字符串

慕姐8265434 2022-12-22 13:47:27
我有一個包含的數組const submissions = [{  question: 'blah blah blah blah',  response: 'response goes here'},我有一個接受提交數組和字符串的函數。我需要檢查響應提交 [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'));如果調用函數 checkString(submissions, 'response goes here') 應該返回 true。這總是返回 false。
查看完整描述

2 回答

?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

您可以簡單地使用一些函數并包含函數,該函數將檢查是否array response包含string您正在檢查的內容。

includes()方法確定數組是否在其條目中包含某個值,返回truefalse適當。

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)


查看完整回答
反對 回復 2022-12-22
?
慕哥9229398

TA貢獻1877條經驗 獲得超6個贊

你可以用一些

在您的代碼片段中,您想檢查響應中是否存在字符串您的代碼無法正常工作,因為您正在使用===所以我的編輯是使用includes

ECMAScript 6 或更高版本:

booleanResult = submissions.some(e => e.response.includes(string))

ECMAScript 5:

booleanResult = submissions.some(function (e) { 
 return e.response.includes(string);
});


查看完整回答
反對 回復 2022-12-22
  • 2 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號