Solved

Comparing Arrays

Posted on 2006-10-19
2
511 Views
Last Modified: 2010-07-27
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
Comment
Question by:gdlp2004
2 Comments
 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 200 total points
ID: 17768523
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
 
LVL 41

Accepted Solution

by:
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now