0

I am wondering how can I combine 2 sources together using the jquery autocomplete?

What I hope to achieve is having the results "chocolate biscuits", "strawberry biscuits", "peanut biscuits" and so on.. then it will loop to swiss roll and start the flavours all over again: "chocolate swiss roll", "strawberry swiss roll", "peanut swiss roll", etc.

When user type the word: "chocolate", all the food values will appear in the autocomplete. However I can't seem to get the results I want. Here is my code:

<script>
var food= [
    "biscuits",
    "swiss roll",
    "bread",
    "waffle",
    "cookies"
];

var flavours= [
    "chocolate",
    "strawberry",
    "peanut",
    "cheese",
    "green tea",
    "vanilla"
];

var mergedResults;

for (i = 0; i < food.length; i++) {
    for (j=0; j < flavours.length; j++) {
        mergedResults = food[i].concat(flavours[j]);
    }
}

$( "#autocomplete" ).autocomplete({
    source: mergedResults
});

</script>

1 Answer 1

1

May be

var mergedResults = [];

for (i = 0; i < food.length; i++) {
    for (j=0; j < flavours.length; j++) {
        mergedResults.push(food[i].concat(" ", flavours[j]));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.