I have implemented a dynamic Ajax call to compare the string entered in the text field (representing a city name) with the "type" value in a JSON array.
As I iterate through the elements of the array, I am checking the values associated with the key "type" to include both cities with a type equal to "city" and those with a type equal to "hamlet".
To prevent duplicate indexing for cities that have both types present, I need to exclude them from being displayed multiple times in the search results.
The Ajax call is as follows:
$.ajax({
url: "https://nominatim.openstreetmap.org/search?q=" + encodeURIComponent($("#inlineFormInputCitta").val()) + "&format=geocodejson",
dataType: "json",
success: function(data) {
var check = false;
for (let i = 0; i < data.features.length; i++) {
let typeCity = data.features[i].properties.geocoding.type;
if (typeCity === "city") {
let nameCity = data.features[i].properties.geocoding.name;
for (let i = 0; i < francigena.tappe.length; i++) {
let tappa = francigena.tappe[i];
let city = francigena.tappe[i].city;
let fs = francigena.tappe[i].fs;
if (city === nameCity && fs === "true") {
check = true;
$('#tabellaEconteuti').show();
} else if (city === nameCity) {
check = true;
console.log("JSON file has been activated");
$('#tabellaEconteuti').show();
$("#tbody").append("<tr><td>" + tappa.name + "</td><td>" + tappa.state + "</td><td>" + tappa.region + "</td><td>" + tappa.city + "</td></tr>");
$("#tabella").show();
}
};
};
}
if (
!check
) {
$('#myModal').modal('show');
}
},
}
The goal is to customize the search by associating user input with the API query URL. However, due to the presence of some cities listed under both "city" and "hamlet" types, there is a concern about duplicates in the search results.
A new method using Set() has been tried:
success: function(data) {
var check = false;
var cityList = new Set()
for (let i = 0; i < data.features.length; i++) {
let typeCity = data.features[i].properties.geocoding.type;
if (typeCity === "hamlet") {
let nameCity = data.features[i].properties.geocoding.name;
for (let i = 0; i < francigena.tappe.length; i++) {
let tappa = francigena.tappe[i];
let city = francigena.tappe[i].city;
let fs = francigena.tappe[i].fs;
if (city === nameCity && fs === "true") {
check = true;
console.log("'fs' === 'true' has been activated");
$('#tabellaEconteuti').show();
$("#tabella").show();
} else if (city === nameCity) {
check = true;
console.log("JSON file has been activated");
$('#tabellaEconteuti').show();
}
if (cityList.has(city)) {
continue
}
cityList.add(city);
if (typeCity === "city") {
let tappa = francigena.tappe[i];
let city = francigena.tappe[i].city;
let fs = francigena.tappe[i].fs;
if (city === nameCity && fs === "true") {
check = true;
console.log("'fs' === 'true' has been activated");
$('#tabellaEconteuti').show();
$('#TrenitaliaButton').on('click', showTrenitaliaInfo);
} else if (city === nameCity) {
check = true;
console.log("JSON file has been activated");
$('#tabellaEconteuti').show();
}
}
}
} {
...
};
}
if (!check) {
$('#tabellaEconteuti').hide();
}
},
Despite attempting to use the Set() method, there were still issues with redundant listings for certain cities. Further adjustments are needed to achieve the desired outcome without duplications.
- -EDIT-
Considering an alternative approach, I aim to leverage the following steps:
1) Identify when the "type" value is "city" and set a flag variable A accordingly.
2) Implement a separate loop to handle cases where the "type" value is "hamlet".
This strategy seems more straightforward and could aid in resolving the duplication problem efficiently. How can I effectively implement this logic?