ok, this is totally breaking my balls, dunno what’s wrong. :wt:
:red: scenario: I have an array with some elements that may repeat themselves or not (this is actually because they’re attributes retrieved from xml nodes, but you don’t need to know that);
:cowbell: what I want to do is: go through that array and check for elements and store them in another array according to the following condition (this is where it gets tricky): if that element does not exist in the final array yet, store it; if it does exist already, nevermind it.
I wrote this code:
//custom method for searching through array
Array.prototype.contains = function(searchValue){
for(i=0; i<this.length; i++){
if (this* == searchValue){
return true
} else {
return false
}
}
}
//final array
cities = [];
//array to be checked for elements
destinations = ["lisbon", "oporto", "oporto", "coimbra", "oporto", "lisbon", "lisbon", "coimbra"];
//loop that does the checking and the pushing if acceptable
for(j=0; j<destinations.length; j++){
item = destinations[j];
if(!cities.contains(item)){
cities.push(item);
}
}
//show me the money
trace(cities);
trace returns lisbon,oporto,oporto,coimbra,oporto,coimbra wich means that the only element in the destinations array being prevented from getting pushed into the final array is the first element in the initial array (in this case, “lisbon”)…
but if it prevents the first one, how come it doesn’t prevent the others, ■■■■ it?.. :upset:
anyone?..