Result picked twice with multiple keys defined #220
-
Hi, I'm trying to query a php script that returns an array with multiple keys (name, surname, id etc...) and my search bar could be set to query by different criterions. I couldn't find a way to reset the autocomplete object every time the search criteria is changed so I worked around making the script return only the results I need and selecting all the possible keys at the same time. But now I found that if a result have the same value in different fields is picked twice... Is there a way to solve this issue? Here is my data field config: data: {
src: async () => {
const source = await fetch("modules/search.php?query=" + $('#search').val() + "&crit=" + currentCrit);
const data = await source.json();
return data;
},
key: [critMode.name, critMode.surname, critMode.id]
// currentCrit is a variable who could assume values from the object critMode
} Some results sample who get picked twice: [{"name": "Alice", "surname": "Alice", "id": "1"}, {"name": "Bob", "surname": "Bob", "id": "2"}] Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hello @ludovi-com, I'm not getting the first part clearly Regarding, getting duplicated results is normal when you use Please, try it and let me know how it goes. {
data: {
src: ... ,
key: [name, surname, id],
results: (list) => {
// Filter duplicates
// incase of multiple data keys usage
const filteredResults = Array.from(new Set(list.map((value) => value.match))).map((id) => {
return list.find((value) => value.match === id);
});
return filteredResults;
},
}
} |
Beta Was this translation helpful? Give feedback.
Hello @ludovi-com,
I'm not getting the first part clearly
reset the autocomplete object every time the search criteria is changed
I would appreciate it if you could explain more on this point maybe I could help you with it.Regarding, getting duplicated results is normal when you use
data.key
so to handle duplicates you need to use the filter snippet below.Please, try it and let me know how it goes.