I need to identify the unique values and put it to another list so that there are no repeated values in the other list. I cannot use functions like .filter() or .includes() or .push() anything like that because I am doing this on Code.org which is what my school uses and they restrict a lot of functions that I normally use.
I've tried:
function contains(city) {
appendItem(filteredLocations, locations[0]);
for (var i = 0; i <locations.length; i );
if (filteredLocations[i] != city){
appendItem(filteredLocations, locations[i]);
}
else {
i ;
}
}`
and this:
function fillDropdowns(){
//filter the cities
for (var i = 0; i < locations.length;i ){
if (contains(locations[i]) == true) {
appendItem(filteredLocations, locations[i]);
}
else {
i ;
}
}
//locations list
}
function contains(city) {
for (var i = 0; i < filteredLocations.length; i );
if (filteredLocations[i] == city){
return true;
}
}
And some others that I have deleted.
CodePudding user response:
You just need to implement a js lookup object to check whether a key is unique or not.
const locations = ["Munich", "Frankfurt", "Nuremberg", "Munich", "Frankfurt"];
const dict = {}; //Look up object
const uniqueCities = [];
for (let i = 0, j = 0; i < locations.length; i) {
const city = locations[i];
if (!dict[city]) { //if city is not in "dict" that means it's an unque city
dict[city] = true; //pushing the unique city to "dict" because it's not unique anymore
uniqueCities[j ] = city;
}
}
console.log(uniqueCities);