-3

Originally, problem is that I must get some notification, when term (city name in project), entered by user in JqueryUI Autocomplete not matching with anything in collection (entered "My Sweet City" and it doesn't match [Moscow, New-York, Beijing]). So I will rewrite my code to manually search in array, but I have one question — how search in array like autocomplete it doing?

2

1 Answer 1

1

There are a couple of ways to do this. If it's something simple, you can get away with using indexOf to find a match like so:

var arr = ["one", "two", "three", "four", "five"];
var inputText = "three";

if (arr.indexOf(inputText) > -1) {
    alert("item found");
}
else {
    alert("item not found");
}

Another option (and more efficient option), would be to use a regular explression and cycle through the array to find matches (mentioned by Aleadam):

function searchStringInArray (str, strArray) {
    for (var j=0; j<strArray.length; j++) {
        if (strArray[j].match(str)) return j;
    }
    return -1;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.