Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to compare three Array's 1

Status
Not open for further replies.

VVVA

Technical User
Sep 19, 2006
35
US
I'm totally new to javascript, in fact I started last week, and so I'm a total noob...

I have three arrays that I want to compare, and find the records that are in all three. Each Array is linked back to drop down boxes in my browser. If I select "High School" from my school dropdown box, "1984" from my second dropdown box, and "male" from my third dropdown box, I want to load "Bob" and "George" into a fourth array that is for another dropdown box.

I am currently doing this by looping through each selected school record, comparing it to each selected year record, and then if I have a match, comparing it to each selected nametype record. This seemed to work fine until I got six thousand different names. Is there a better way to compare arrays?

Code:
Schools['High School'][0]='Bob'
Schools['High School'][1]='George'
Schools['Middle School'][0]='Jill'
Schools['Middle School'][1]='Joseph'
Schools['Pre-School'][0]='Larry'

Years['1984'][0]='Bob'
Years['1984'][1]='George'
Years['1985'][0]='Jill'
Years['1985'][1]='Joseph'
Years['1986'][0]='Larry'

NameType['Male'][0]='Bob'
NameType['Male'][1]='George'
NameType['Crossdresser'][0]='Jill'
NameType['Female'][0]='Joseph'
NameType['Female'][0]='Larry'
 
Why not create an array of objects with 4 properties and then just sort through the one array?
Code:
var student = new Array(), si=0;

student[si++] = {Schools:'High School',Years:'1984',NameType:'Male',Name:'Bob'};
student[si++] = {Schools:'High School',Years:'1984',NameType:'Male',Name:'George'};
student[si++] = {Schools:'Middle School',Years:'1985',NameType:'Crossdresser',Name:'Jill'};
student[si++] = {Schools:'Middle School',Years:'1985',NameType:'Female',Name:'Joseph'};
student[si++] = {Schools:'Pre-School',Years:'1986',NameType:'Female',Name:'Larry'};

var found = new Array(), fi=0;

for (si=0;si<student.length;si++)
  {
  if (student[si].Schools == 'High School')
    {
    if (student[si].Years == '1984')
      {
      if (student[si].NameType == 'Male')
        {
        found[fi] = student[si];
        fi++;
        }
      }
    }
  }

If you want to make the code faster, you could optimize the loop by unrolling it and put the comparison into a separate function. I'd highly recommend this kind of optimization if you have 6000 students to sort through.

Lee
 
Hey, I just wanted to say really appreciate the time you took to answer my question. Your solution worked perfect and saved me hours. I didn't realize you could do Arrays like tables. Anyway, kudos for the answer, I really appreciate it!
 
Did you look up what unrolling loops is? I've found using that technique fantastically increases the speed of handling an array. You'd want to split your comparison into a function to make things less cluttered and the code easy to read.

I typically use the following format.
Code:
var over = arr.length % 10, offset = 0;

for (var ai=0;ai<over;ai++)
  {
  if (checkarrayelement(arr[ai])) {/*do something*/};
  }

for (var ai=0;ai<over;ai+=10)
  {
  offset=0;
  if (checkarrayelement(arr[ai + offset])) {/*do something*/};
  
  offset++;
  if (checkarrayelement(arr[ai + offset])) {/*do something*/};
  
  offset++;
  if (checkarrayelement(arr[ai + offset])) {/*do something*/};
  
  offset++;
  if (checkarrayelement(arr[ai + offset])) {/*do something*/};
  
  offset++;
  if (checkarrayelement(arr[ai + offset])) {/*do something*/};
  
  offset++;
  if (checkarrayelement(arr[ai + offset])) {/*do something*/};
  
  offset++;
  if (checkarrayelement(arr[ai + offset])) {/*do something*/};
  
  offset++;
  if (checkarrayelement(arr[ai + offset])) {/*do something*/};
  
  offset++;
  if (checkarrayelement(arr[ai + offset])) {/*do something*/};
  
  offset++;
  if (checkarrayelement(arr[ai + offset])) {/*do something*/};
  }

Lee
 
Wow, that is cool! The loop handles ten records a pop instead of one. I'm going to save your template. Right now, my page is bottlenecking worse at the point when it loads images than when it is checking the arrays, thanks to your solution. If I keep messing with arrays in javascript though, this'll really come in handy, thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top