var unique = [];
for(var i = 0; i < array1.length; i++){
var found = false;
for(var j = 0; j < array2.length; j++){ // j < is missed;
if(array1[i] == array2[j]){
found = true;
break;
}
}
if(found == false){
unique.push(array1[i]);
}
}
UPDATE
The original post works but is not very fast, this implementation is a lot faster, this example uses only one array, but in the function you can easily pass any additional arrays to combine.
only a simple error check is done and more should be in place, use at own discretion, meant as working example only.
function Unique(array) {
var tmp = [];
var result = [];
if (array !== undefined /* any additional error checking */ ) {
for (var i = 0; i < array.length; i++) {
var val = array[i];
if (tmp[val] === undefined) {
tmp[val] = true;
result.push(val);
}
}
}
return result;
}
var unique = Unique([1, 2, 2, 6, 8, 5, 6, 8]);
Additionally this can be implemented as prototype of the Array object, changing the signature to
Array.prototype.Unique = function() {
var array = this;
...
}
and can be called like this:
var unique = [1, 2, 2, 6, 8, 5, 6, 8].Unique();