Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript compare arrays remove duplicates

var arr1 = [1, 2, 3, 4, 5];
var arr2 = [1, 3, 5, 7, 9];


arr2 = arr2.reduce(function (prev, value) {

    var isDuplicate = false;
    for (var i = 0; i < arr1.length; i++) {
        if (value == arr1[i]) {
            isDuplicate = true;
            break;
        }
    }
      
    if (!isDuplicate) {
        prev.push(value);
    }
       
    return prev;
        
}, []);


alert(JSON.stringify(arr2));
 Run code snippet
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #compare #arrays #remove #duplicates
ADD COMMENT
Topic
Name
3+8 =