Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 538
  • Last Modified:

Comparing Arrays

Hello.
I have been working with this a little while and because I am a little "rusty" with my javascript, I can't figure it out. Here is what I am trying to do, I have to arrays:

testSpecifyList = ('APR','DRE','TAB');
specCompareArray = ('APR','DRE','DRE');

I am trying to compare testSpecifyList and specCompareArray in two different ways. The first being if specCompareArray has any duplicates. (FYI, testSpecifyList will NEVER have duplicates so I don't need to check that.)

The second is, is there a one to one with testSpecifyList to specCompareArray.  Does everything in testSpecifyList match what is in specCompareArray? What would be the best is if you could leave the array with the one that doesn't exists. So, using the example it would look something like this:

     if(testSpecifyList[0]==specCompareArray [0])
           testSpecifyList[0]=null;//not sure if you can do this
           specCompareArray [0]=null;

So at the end of the loop it would display:
testSpecifyList.length=0
specCompareArray.length=1

Any help is greatly appreciated. Thanks in advance!
0
gdlp2004
Asked:
gdlp2004
2 Solutions
 
rama_krishna580Commented:
Hi,

look at here..
http://www.webdeveloper.com/forum/showthread.php?t=82393

Array.compareArrays <array1>.compareArrays(<array2>)
Normal JavaScript doesn't compare arrays in a way that's sometimes handy. Using the following [1,2] == [1,2], will give you false, quite opposite what you most likely intended. This method let's you compare arrays, based on their contents. Also note, that this function handles nested arrays just fine also.

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20914416.html

R.K
0
 
HonorGodCommented:
//--------------------------------------------------
// Name: anyDups()
// Role: Return true of any two elements of the specified array are equal
//--------------------------------------------------
function anyDups( a ) {
  for ( var i = 1; i < a.length - 1 ; ++i ) {
    for ( var j = i + 1; j < a.length; ++ j ) {
      if ( a{ i ] == a[ j ] ) return true;
    }
  }
  return false;
}
   
//--------------------------------------------------
// Name: matches()
// Role: Return true of corresponding elements of two arrays match
//--------------------------------------------------
function matches( a, b ) {
  var less = min( a.length, b.length );
  for ( var i = 0; i < less; ++i ) {
    if ( a[ i ] == b[ i ] )  return true;
  }
  return false;
}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now