8

With 2 large, potentially very large nested javascript arrays. One is current and the other is previous iteration of this array. The function will need to find all differing elements and act upon anything changed.

I know how to make a function to do this, I am wondering what the Best Practices are for doing such a thing. Any good advice will be appreciated. I am looking at using either native JavaScript with jQuery handling the responses to the differing elements.

This Question deals with several things.

  1. What is the most efficient way to compare objects. In javascript checking, via if, if a an object equals or does not equal another object, will always say it does not, even if they are equal. Thus the objects need to be broken down and compared.

  2. What is the best way to return the results? Do you make an array of the differences? While going though the first array do you clear out objects that are the same as they are in the first, or make an entirely new array to return?

djangofan
  • 28,471
  • 61
  • 196
  • 289
Case
  • 4,244
  • 5
  • 35
  • 53
  • Check out how The lodash library does it.https://github.com/bestiejs/lodash – lucuma May 13 '13 at 02:01
  • Differing elements, as in different in the order they appear when comparing the 2? Or are we looking at what the 2 arrays contain in total, irrelevant of order? Perhaps you could show us what you are currently doing and then you could get some opinions of best practice. Unfortunately this question currently suffers from "not a real question" syndrome. – Xotic750 May 13 '13 at 02:34
  • Duplicate of ["What is the fastest or most elegant way to compute a set difference using Javascript arrays?"](http://stackoverflow.com/questions/1723168/what-is-the-fastest-or-most-elegant-way-to-compute-a-set-difference-using-javasc) and of ["JavaScript array difference"](http://stackoverflow.com/questions/1187518/javascript-array-difference). – Brock Adams May 13 '13 at 05:35
  • Neither of those Brock deal with nested arrays. That is just how to, as stated in the topic I know how to. I want to know what the best method for doing so is. – Case May 13 '13 at 14:42

2 Answers2

1
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 look at the second given. And modify it removing any shared equal values. The array will still be intact but any values that were the same are now undefined.

Billy
  • 886
  • 8
  • 18
0

Personally, i think recursion is a good practice for this situation.

console.clear();
// Compare 2 nested arrays, do something if values don't match
function CompareArrays(arr1, arr2){

    for(var i=0;i<arr1.length;i++){
        if( typeof arr1[i] === "object"){
            // traverse into nested array
            CompareArrays( arr1[i], arr2[i] );
        }else{
            if (arr1[i] != arr2[i]){
                // do something!
                console.log( "mismatch @ "+ i +" a1: "+arr1[i]+" a2: "+ arr2[i]);
            }
        }
    }
}

var a1 = [1,2,3,["a","b","c"],4,5,6,["d","e","f"]];
var a2 = [1,2,55,["a","tt","c"],4,5,6,["d","e","f"]];

CompareArrays( a1, a2);

working fiddle: http://jsfiddle.net/ymSmP/5

carrabino
  • 1,742
  • 1
  • 14
  • 17