javascript - Compare arrays of different sizes and dimensions -
so here's proposed problem.
compare 2 arrays , return new array items not found in both of original arrays.
here's have far.
function diff(arr1, arr2) {     (var in arr1) {         (var b in arr2) {             if (arr1[a] == arr2[b]){                 arr2.splice(b,1);             }         }     }     return arr2; }  diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);   this code compares each value of first array second one. if match found removes item using splice function.
this works great arrays 1 dimensional how can work multidimensional arrays such as:
diff([1, 2, 3, 5], [1, [2, 3], [4, 5]]);   what if these arrays not 2 dimensions number of dimensions. should able iterate through every element of every array no matter set up.
with lodash can :
var = [1, 2, 3, 5, 7], b = [1, [2, 3], [4, 5, [7]]];    var result = _.filter(_.flattendeep(b), function(item){    return a.indexof(item) === -1;  });    console.log(result);  $("body").append(json.stringify(result))  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.0/lodash.min.js"></script>  <body></body>  
Comments
Post a Comment