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

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

檢查json是否包含key

檢查json是否包含key

月關寶盒 2023-04-14 17:31:41
我想搜索一個 JSON 文件,看看它是否包含我要搜索的電子郵件地址(下面代碼中的 [email protected])。但是,它不起作用。function contains(arr, key, val) {  for (var i = 0; i < arr.length; i++) {      if(arr[i][key] === val) return true;  }  return false;}emailRecord = JSON.parse(req.responseText); //Got this from an XMLHttpRequest.if(contains(fullEmailRecord, "email", "[email protected]")) {   console.log("LOGGED IN")}JSON:[  {    "email": "[email protected]"  },  {    "email": "[email protected]"  }]function contains(arr, key, val) {  for (var i = 0; i < arr.length; i++) {      if(arr[i][key] === val) return true;  }  return false;}const fullEmailRecord = [  {    "email": "[email protected]"  },  {    "email": "[email protected]"  }];if(contains(fullEmailRecord, "email", "[email protected]")) {   console.log("LOGGED IN")}
查看完整描述

3 回答

?
www說

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

我認為它比這簡單得多,在字符串而不是 json 中搜索它,如下所示:

req.responseText.contains("[email protected]")

或者

req.responseText.indexOf("[email protected]") > -1

完整使用:

if (req.responseText.contains("[email protected]")) { 
  console.log("LOGGED IN")
}


查看完整回答
反對 回復 2023-04-14
?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

它在代碼片段中有效,但在原始代碼段中無效,因為fullEmailRecord使用的是變量而不是emailRecord .



查看完整回答
反對 回復 2023-04-14
?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

做這個:

arr.map(e => e.email === 'YOUR EMAIL');

如果返回一個對象那么它存在

換句話說,你可以改變你的功能

function contains(arr, key, val) {
  return arr.map(e => e[key] === val)
}


查看完整回答
反對 回復 2023-04-14
  • 3 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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