function CompareArrays(arr1, arr2){ for(var key in arr1){ if( arr1[key] !== arr2[key]){ // traverse into nested array if(typeof(arr1[key]) == 'object' || typeof(arr2[key]) == 'object'){ CompareArrays( arr1[key], arr2[key]); } }else{ delete arr2[key]; } } } var a1 = [1,2,3,["a","b","c"],4,5,6,["d","e","f"]]; var a2 = [1,2,5445,["a","tt","c"],4,5,336,["d","edee","ffdf"], 'blablabla', 'I\'m extra']; CompareArrays( a1, a2 ); console.log(a2);
This will consider the second. And change it by removing any common equal values. The array will still be intact, but any values ββthat were the same are now undefined.
Billy
source share