I have an input that uses an API to fetch some cities based on the letters inserted. The API is launched on each keyup event like so :
let ville_input = document.getElementById("search_immobilier_ville");
let ville_arr = [];
ville_input.addEventListener("keyup", () => {
res_list.innerHTML = "";
fetch("https://api.fr/communes?nom=" + ville_input.value)
.then(res => {
return res.json();
})
.then(data => {
data.forEach(el => {
if (
el.codeDepartement == "971" &&
el.nom
.toUpperCase()
.startsWith(ville_input.value.toUpperCase().trim())
) {
if (!ville_arr.includes([el.nom, el.codesPostaux[0]])) {
ville_arr.push([el.nom, el.codesPostaux[0]]);
console.log(ville_arr);
}
}
});
})
.catch(err => {
// Doing stuff
});
});
First of all I want to push each results as arrays into an array like so :
ville_arr.push([el.nom,el.codesPostaux[0]])
My issue is that I get duplicate items into my array when the API fetch the same result, that is why I tried this :
if(!ville_arr.includes([el.nom,el.codesPostaux[0]])){
ville_arr.push([el.nom,el.codesPostaux[0]])
console.log(ville_arr)
}
But I still get duplicate items in the end, I guess it has something to do with the indexes of the array which are unique ? maybe something else ?
