?
Solved

Comparing Arrays

Posted on 2006-10-19
2
Medium Priority
?
528 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 800 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 800 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

777 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