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?

4
  • Check out how The lodash library does it.github.com/bestiejs/lodash Commented May 13, 2013 at 2: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. Commented May 13, 2013 at 2:34
  • Duplicate of "What is the fastest or most elegant way to compute a set difference using Javascript arrays?" and of "JavaScript array difference". Commented May 13, 2013 at 5: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. Commented May 13, 2013 at 14:42

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

This assumes that all keys in a1 will be in a2. And that a2 will not have differing keys.
yes of course. You didn't say that the arrays would be different sizes too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.