Solved

Comparing Arrays

Posted on 2006-10-19
2
518 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

828 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