Check for existence in array and then store

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?..

daddy always told me “son, if you weren’t such a slow, hard-headed, dumb little bugger, maybe you’d have a chance to get somewhere in life…”

the code should have been

Array.prototype.contains = function(searchValue) {
    for (i=0; i<this.length; i++) {
        if (this* == searchValue) {
            return true;
        }
    }
    return false;
}

instead of

Array.prototype.contains = function(searchValue){
    for(i=0; i<this.length; i++){
        if (this* == searchValue){
            return true
        } else {
            return false
        }
    }
}

now, if you don’t mind, I’m going to hit my head hard against the wall… :hangover:

You can try using the following, which will sort them for you:

function arrayUnique(arrayName:Array) {
	var arrayName:Array = arrayName.slice().sort();
	var i:Number = arrayName.length;
	while (--i > 0) {
		while (arrayName* == arrayName[i - 1]) {
			arrayName.splice(i - 1, 1);
		}
	}
	return arrayName;
}
destinations = ["lisbon", "oporto", "oporto", "coimbra", "oporto", "lisbon", "lisbon", "coimbra"];
var cities:Array = arrayUnique(destinations);
trace (destinations);
trace (cities);

If you don’t actually want to create a new array, change the first line of the function to var arrayName:Array = arrayName.sort(); and then check

destinations = ["lisbon", "oporto", "oporto", "coimbra", "oporto", "lisbon", "lisbon", "coimbra"];
trace (destinations);
destinations = arrayUnique(destinations);
trace (destinations);