如果只给出部分数量的标识符,我可以在循环解析的 Json 输出以查找特定对象时使用 indexOf 吗?
基本上,我正在调用 Betfair API 来获取 selectionIds,但有时我只有玩家的姓氏,而不是 json 响应提供的全名。我想我需要这样的东西,但无法弄清楚。Json 数据是
var response = { jsonrpc: '2.0',
result:
[ { marketId: '1.118739296',
marketName: 'Match Odds',
marketStartTime: '2015-05-13T01:00:00.000Z',
totalMatched: 17,
runners:
[ { selectionId: 7750118,
runnerName: 'Makoto Ninomiya',
handicap: 0,
sortPriority: 1,
metadata: { runnerId: '7750118' } },
{ selectionId: 7659425,
runnerName: 'Su Jeong Jang',
handicap: 0,
sortPriority: 2,
metadata: { runnerId: '7659425' } } ],
eventType: { id: '2', name: 'Tennis' },
competition: { id: '7354189', name: 'ITF Women Kurume 2015' },
event:
{ id: '27442711',
name: 'Ninomiya v Jang',
countryCode: 'JP',
timezone: 'Japan',
openDate: '2015-05-13T01:00:00.000Z' } } ],
id: 1 }
代码是
for (var i = 0; i<= Object.keys(response.result).length; i++ ) {
if(response.result[0].runners[i].runnerName == 'Su Jeong Jang'){
document.body.innerHTML = response.result[0].runners[i].selectionId;
}
}
这发现 selectionID 很好,但如果我只使用这样的姓氏,我不会得到匹配
for (var i = 0; i<= Object.keys(response.result).length; i++ ) {
if(response.result[0].runners[i].runnerName.indexOf('Su Jeong') >0 {
document.body.innerHTML = response.result[0].runners[i].selectionId;
}
}
谁能指出我正确的方向?非常感谢。