开发者

Searching for character in JSON using JavaScript

[
{"lastName":"Noyce","gender":"Male","patientID":19389,"firstName":"Scott","age":"53Y,"}, 
{"lastName":"noyce724","gender":"Male","patientID":24607,"firstName":"rita","age":"0Y,"}
]

Comparing my input with the JSON.

for (var i = 0; i < recentPa开发者_如何学编程tientsList.length; ++i) {
  if (searchBarInput === recentPatientsList[i].lastName) {
    alert("Found at index " + i);
  }
}

With this i am able to see whether i match my input with the JSON. How can i get results of data whose last name starts with 'N' or given input.


Assuming you searchBarInput is equal to "n", Try this:

var results = []; // initialize array of results
for (var i = 0; i < recentPatientsList.length; ++i) {
  if (recentPatientsList[i].lastName.indexOf(searchBarInput) == 0) {
    results.push(recentPatientsList[i].lastName);
  }
}
alert('Results: ' + results.toString());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜