用小写和大写搜索字符

时间:2011-04-23 22:36:33

标签: javascript json

我的JSON数据

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

var searchBarInput = TextInput.value;
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());

我得到了结果,但是当我搜索一个以单词开头的字符时,我希望它同时匹配大写和小写。

2 个答案:

答案 0 :(得分:2)

您可以在两个字符串上使用toUpperCase

if (recentPatientsList[i].lastName.toUpperCase().indexOf(searchBarInput.toUpperCase()) == 0) {

答案 1 :(得分:1)

if(recentPatientsList[i].lastName.toLowerCase().indexOf(searchBarInput.toLowerCase()) == 0)