473,570 Members | 2,995 Online

# Comparing Multidimensiona l Arrays

11 New Member
Hello everybody, my question is this:
I have two multidimensiona l arrays and I have to create a third one (for later use) from comparing these two. Here is my example code:
Expand|Select|Wrap|Line Numbers
1. //BEGIN CODE
2. var myGroups = [["00002","00003","00004","00005"],["00007","00008"],["00009","00010"]];
3. var myProducts = [["00002"],["00003"],["00007"],["00009"]];
4. var newGroups = new Array();//Resulting Array
5. function main() {
6.
7. for(i=0; i<myGroups.length; i++){
8.
9. var myGroup = myGroups[i];
10.
11. for(k=0; k<myGroup.length; k++){
12.
13. for(j=0; j<myProducts.length; j++){
14.
15. if(myProducts[j]==myGroup[k]){
16.
17. newGroups.push(new Array(myProducts[j]));
18.
19. }
20. }
21. }
22. }
24.
25. }
26. main();
27.
This way I get a third multidimensiona l array (newGroups) which contains 1 line arrays, while I want to maintain the grouping structure of the first multidimensiona l array ([["00002","00 003"],["00007"],["00009"]]

Hope you understand the problem..
Any suggestions would be very appreciate
Thanks
Nov 1 '08 #1
12 5631
acoder
16,027 Recognized Expert Moderator MVP
If you want to keep the structure, create the array outside the third nested loop and populate within it (if statement - line 15-17), then push the array outside that loop.
Nov 2 '08 #2
filippo nanni
11 New Member
Thank you acoder, I tried what you're saying but I'm afraid I'm doing something wrong - Should you please hard code your solution? It would be very helpful for me (I'm not an expert coder..). Anyway, thank you so much for your help.
Nov 2 '08 #3
acoder
16,027 Recognized Expert Moderator MVP
Can you show me what you tried?
Nov 3 '08 #4
filippo nanni
11 New Member
Sure, here's my attempt:
Expand|Select|Wrap|Line Numbers
1. //BEGIN CODE
2. var myGroups = [["00002","00003","00004","00005"],["00007","00008"],["00009","00010"]];
3. var myProducts = [["00002"],["00003"],["00007"],["00009"]];
4. var resultingGroups = new Array();//Resulting Array
5. function main() {
6.     for(i=0; i<myGroups.length; i++){
7.         var myGroup = myGroups[i];
8.         for(k=0; k<myGroup.length; k++){
9.             var newGroup = new Array();//Array outside third nested loop
10.             for(j=0; j<myProducts.length; j++){
11.                 if(myProducts[j]==myGroup[k]){
12.                     newGroup.push(new Array(myProducts[j]));//Populates newGroup
13.                 }
14.             }
15.             resultingGroups.push(newGroup);//Push resultingGroups with newGroup
16.         }
17.     }
18. }
19. main();
21.
Nov 3 '08 #5
acoder
16,027 Recognized Expert Moderator MVP
I haven't tested your code, but I noticed one thing. On line 12, you're pushing an array making newGroup into a multi-dimensional array. Just push myProduct[j] instead.
Nov 3 '08 #6
filippo nanni
11 New Member
Hello acode, I tried to modify the code as you suggested me; I also expanded a little the code to make the result more evident; what I get is something like this:

Comparing Multidimensiona l Arrays

myGroup_0
00002
00003
00004
00005
myGroup_1
00007
00008
myGroup_2
00009
00010
resultingGroup_ 0
00002
resultingGroup_ 1
00003
resultingGroup_ 2
resultingGroup_ 3
resultingGroup_ 4
00007
resultingGroup_ 5
resultingGroup_ 6
00009
resultingGroup_ 7
[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
<body>
<script language="JavaS cript">
<!--
//BEGIN CODE
var myGroups = [["00002","00003" ,"00004","00005 "],["00007","00 008"],["00009","00 010"]];
var myProducts = [["00002"],["00003"],["00007"],["00009"]];
var resultingGroups = new Array();//Resulting Array
function main() {

for(i=0; i<myGroups.leng th; i++){
var myGroup = myGroups[i];
for(k=0; k<myGroup.lengt h; k++){
var newGroup = new Array();//Array outside third nested loop
for(j=0; j<myProducts.le ngth; j++){
if(myProducts[j]==myGroup[k]){
newGroup.push(m yProducts[j]);//Populates newGroup
}
}
resultingGroups .push(newGroup) ;//Push resultingGroups with newGroup
}
}

}
main();
for(i=0; i<myGroups.leng th; i++){
document.write( "<br>"+"myGroup _"+i);
var myGroup = myGroups[i];
for(k=0; k<myGroup.lengt h; k++){
document.write( "<br>"+myGr oup[k]);
}
}

for(i=0; i<resultingGrou ps.length; i++){
document.write( "<br>"+"resulti ngGroup_"+i);
var myGroup = resultingGroups[i];
for(k=0; k<myGroup.lengt h; k++){
document.write( "<br>"+myGr oup[k]);
}
}
//-->
</script>
</body>
</html>[/HTML]

Have you got any suggestions?

Thanks
Nov 3 '08 #7
acoder
16,027 Recognized Expert Moderator MVP
You need to move the array creating and populating lines further out, i.e. move line 17 upwards outside the for loop and line 23 downwards outside the loop.
Nov 3 '08 #8
filippo nanni
11 New Member
Great acode, it worked!!!
I include the final code for the sake of completeness:

[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
<body>
<script language="JavaS cript">
<!--
//BEGIN CODE
var myGroups = [["00002","00003" ,"00004","00005 "],["00007","00 008"],["00009","00 010"]];
var myProducts = [["00002"],["00003"],["00007"],["00009"]];
var resultingGroups = new Array();//Resulting Array
function main() {
for(i=0; i<myGroups.leng th; i++){
var myGroup = myGroups[i];
var newGroup = new Array();
for(k=0; k<myGroup.lengt h; k++){
for(j=0; j<myProducts.le ngth; j++){
if(myProducts[j]==myGroup[k]){
newGroup.push(m yProducts[j]);//Populates newGroup
}
}
}
resultingGroups .push(newGroup) ;//Push resultingGroups with newGroup
}
}
main();
for(i=0; i<myGroups.leng th; i++){
document.write( "<br>"+"myGroup _"+i);
var myGroup = myGroups[i];
for(k=0; k<myGroup.lengt h; k++){
document.write( "<br>"+myGr oup[k]);
}
}

for(i=0; i<resultingGrou ps.length; i++){
document.write( "<br>"+"resulti ngGroup_"+i);
var myGroup = resultingGroups[i];
for(k=0; k<myGroup.lengt h; k++){
document.write( "<br>"+myGr oup[k]);
}
}
//-->
</script>
</body>
</html>[/HTML]

Thank you again for the great support!

Filippo
Nov 4 '08 #9
acoder
16,027 Recognized Expert Moderator MVP
No problem at all! Glad to hear that it's working. Remember that should you need help on any JavaScript-related issues in future, you know where to come :)
Nov 4 '08 #10