We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26310
    • 130 Posts
    hello,

    does anyone out there have an easy way to compare two arrays in javascript? I’m looking to compare array1 and array 2 and return what the difference...

    Array two will have elements added to it as well as what was in array 1 but I need to figure out how to tell what was added to array 2. I’m totally stuck here and don’t know if this is feasible.
      I twitch because I care....and drink too much coffee.
      • 16702 ☆ A M B ☆
      • 536 Posts
      http://www.webdeveloper.com/forum/showthread.php?t=82393
      http://bytes.com/topic/javascript/insights/699379-optimize-loops-compare-two-arrays
      // --------------------------------
      // ARRAY COMPARE - V. 2
      // Compare two arrays and insert
      // the result (biggest array,
      // elements of arrays) in a function
      // 
      // Author: Davide Beltrame (Broly)
      // Mail: [email protected]
      // 
      // Thanks to Sephiroth (http://www.sephiroth.it) for the idea
      // of the new usage of the array
      // properties in the result
      // --------------------------------
       
      Array.prototype.compare = function () { 
      var diff, second, c, result, arrel1, arrel2;
       
      second = arguments[0];
       
      diff = this.length-second.length>0 ? this 
      : this.length-second.length != 0 ? second : "Arrays are equals";
       
      second = diff == this ? second : this;
       
      diff.arrel1 = new Array();
       
      for (c=0; c<diff.length; c++) {
      diff.arrel1.push("Element"+(c+1)+" of Array1 = "+diff[c])
      diff.arrel1.push("Type of element"+(c+1)
      +" of Array1 = "+typeof (diff[c]))
      }
       
       
      diff.arrel2 = new Array();
      for (c=0; c<second.length; c++) {
      diff.arrel2.push( second[c] );
      diff.arrel2.push( typeof(second[c]));
      }
       
      result = new Array();
       
      result.biggest = diff;
      result.biggest.length = diff.length;
      result.biggest.elements = diff.arrel1;
       
      result.second = second;
      result.second.length = second.length;
      result.second.elements = diff.arrel2;
       
      return result;
       
      }
      // Usage samples
      arrayA = new Array("A", "B", "C", "D", "E", "F");
      arrayB = new Array(1, 2, 3, 4, 5, 6, 7);
       
      // Trace a properties
      trace ("Biggest array: " + arrayA.compare(arrayB).biggest);
      trace ("Second array: " + arrayA.compare(arrayB).second);
      trace ("Type of 1st second Array element: " 
      + arrayA.compare(arrayB).second.elements[1]);
      trace("")
      // Assign a properties to a var
      a = arrayA.compare(arrayB).second.length;
      trace("A value: " + a)
       
       
      // Usage instructions
      // 
      // Syntax: 
      // 1st Array to compare.compare(2nd Array to compare)
      // (example: ArrayA.compare(ArrayB)
      // 
      // New syntax for return the properties in this version
      // 
      // The syntax is:result.(array).(property)
      // For example: result.biggest.length return the 
      // lenght of biggest array
      // result.second.lentgh return the lenght of other array
      // 
      // Array parameter can have one of this values
      //
      // biggest <- the biggest array
      // second <- other array
      // 
      // PROPERTIES
      // 
      // length <- length of the array
      // elements <- list of elements and their type 
      // (you can return only an element or his type
      // with this syntax: result.(array).elements[n]
      // For example
      // result.biggest.elements[0] 
      <- return the 1st biggest array element
      // result.biggest.elements[1] 
      <- return the type of 1st biggest array element
      // Using result.(array).elements, 
      Flash return a list of all elements (and their type) in the array

        palma non sine pulvere
        • 26310
        • 130 Posts
        wow thanks. the second link is the link I ended up using.

        				//1) compare arrays
        				var lookup = {};		
        				for (var j in day3Arr) {
        					lookup[day3Arr[j]] = day3Arr[j];
        				}	
        				// check
        				for (var i in result3) {
        					if (typeof lookup[result3[i]] == "undefined") {// if it's undefined, it's new
        						alert('NEW! ' + result3[i] + ' is new');
        
        					}// end if
        				}// end for			
        


        Again thanks for the response, that script will come in handy for a project coming up where I need a bit more extensive comparison.
          I twitch because I care....and drink too much coffee.