Solved

# Comparing Arrays

Posted on 2006-10-19
511 Views
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
Question by:gdlp2004

LVL 23

Assisted Solution

rama_krishna580 earned 200 total points
ID: 17768523
Hi,

look at here..

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

LVL 41

Accepted Solution

HonorGod earned 200 total points
ID: 17771084
//--------------------------------------------------
// 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

### Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…