0
[
{"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 < recentPatientsList.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.

2
  • Did you ask the same question twice?? Commented Apr 23, 2011 at 21:18
  • not exactly, but its some what similar Commented Apr 23, 2011 at 21:23

1 Answer 1

1

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());
Sign up to request clarification or add additional context in comments.

4 Comments

Wbat does indexOf(inputControl) == 0 means? Why do we check with a Zero.
I don't know what inputControl is but indexOf() returns the index of the matching string -- 0 if the match is at the beginning.
its a textInput... and if i have to match for n in any part of the word... what should i use.
If there is a match, then the indexOf() method will return a positive number (or zero) -- and -1 if there is no match. So you can check to see if the result is >= 0 or != -1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.