469,125 Members | 1,676 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,125 developers. It's quick & easy.

Comparing Multidimensional Arrays

Hello everybody, my question is this:
I have two multidimensional 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. }
  23. alert (newGroups[0]);
  24.  
  25. }
  26. main();
  27.  
This way I get a third multidimensional array (newGroups) which contains 1 line arrays, while I want to maintain the grouping structure of the first multidimensional array ([["00002","00003"],["00007"],["00009"]]

Hope you understand the problem..
Any suggestions would be very appreciate
Thanks
Nov 1 '08 #1
12 5114
acoder
16,027 Expert Mod 8TB
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
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 Expert Mod 8TB
Can you show me what you tried?
Nov 3 '08 #4
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();
  20. alert (resultingGroups[0]);
  21.  
Nov 3 '08 #5
acoder
16,027 Expert Mod 8TB
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
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 Multidimensional 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.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
<head><h2>Comparing Multidimensional Arrays</h2></head>
<body>
<script language="JavaScript">
<!--
//BEGIN CODE
var myGroups = [["00002","00003","00004","00005"],["00007","00008"],["00009","00010"]];
var myProducts = [["00002"],["00003"],["00007"],["00009"]];
var resultingGroups = new Array();//Resulting Array
function main() {

for(i=0; i<myGroups.length; i++){
var myGroup = myGroups[i];
for(k=0; k<myGroup.length; k++){
var newGroup = new Array();//Array outside third nested loop
for(j=0; j<myProducts.length; j++){
if(myProducts[j]==myGroup[k]){
newGroup.push(myProducts[j]);//Populates newGroup
}
}
resultingGroups.push(newGroup);//Push resultingGroups with newGroup
}
}

}
main();
for(i=0; i<myGroups.length; i++){
document.write("<br>"+"myGroup_"+i);
var myGroup = myGroups[i];
for(k=0; k<myGroup.length; k++){
document.write("<br>"+myGroup[k]);
}
}

for(i=0; i<resultingGroups.length; i++){
document.write("<br>"+"resultingGroup_"+i);
var myGroup = resultingGroups[i];
for(k=0; k<myGroup.length; k++){
document.write("<br>"+myGroup[k]);
}
}
//-->
</script>
</body>
</html>[/HTML]

Have you got any suggestions?

Thanks
Nov 3 '08 #7
acoder
16,027 Expert Mod 8TB
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
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.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
<head><h2>Comparing Multidimensional Arrays</h2></head>
<body>
<script language="JavaScript">
<!--
//BEGIN CODE
var myGroups = [["00002","00003","00004","00005"],["00007","00008"],["00009","00010"]];
var myProducts = [["00002"],["00003"],["00007"],["00009"]];
var resultingGroups = new Array();//Resulting Array
function main() {
for(i=0; i<myGroups.length; i++){
var myGroup = myGroups[i];
var newGroup = new Array();
for(k=0; k<myGroup.length; k++){
for(j=0; j<myProducts.length; j++){
if(myProducts[j]==myGroup[k]){
newGroup.push(myProducts[j]);//Populates newGroup
}
}
}
resultingGroups.push(newGroup);//Push resultingGroups with newGroup
}
}
main();
for(i=0; i<myGroups.length; i++){
document.write("<br>"+"myGroup_"+i);
var myGroup = myGroups[i];
for(k=0; k<myGroup.length; k++){
document.write("<br>"+myGroup[k]);
}
}

for(i=0; i<resultingGroups.length; i++){
document.write("<br>"+"resultingGroup_"+i);
var myGroup = resultingGroups[i];
for(k=0; k<myGroup.length; k++){
document.write("<br>"+myGroup[k]);
}
}
//-->
</script>
</body>
</html>[/HTML]

Thank you again for the great support!

Filippo
Nov 4 '08 #9
acoder
16,027 Expert Mod 8TB
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
Actually, there is another question that comes to my mind -
With the actual code, a create a new array (newGroup) even if there is no correspondence between the two starting arrays (myGroups, myProducts). I'm thinking if there is a way to create a new array only when there is a correspondence between the two..
Nov 4 '08 #11
acoder
16,027 Expert Mod 8TB
On line 23, check if the array is not empty before adding it to resultingGroups.
Nov 4 '08 #12
Sure acode, you're right, I should have think it by myself..
finished code attached

[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
<head><h2>Comparing Multidimensional Arrays</h2></head>
<body>
<script language="JavaScript">
<!--
//BEGIN CODE
var myGroups = [["00002","00003","00004","00005"],["00007","00008"],["00009","00010"]];
var myProducts = [["00002"],["00003"],["00009"]];
var resultingGroups = new Array();//Resulting Array
function main() {
for(i=0; i<myGroups.length; i++){
var myGroup = myGroups[i];
var newGroup = new Array();
for(k=0; k<myGroup.length; k++){
for(j=0; j<myProducts.length; j++){
if(myProducts[j]==myGroup[k]){
newGroup.push(myProducts[j]);//Populates newGroup
}
}
}
if(newGroup.length > 0){
resultingGroups.push(newGroup);//Push resultingGroups with newGroup
}
}
}
main();
for(i=0; i<myGroups.length; i++){
document.write("<br>"+"myGroup_"+i);
var myGroup = myGroups[i];
for(k=0; k<myGroup.length; k++){
document.write("<br>"+myGroup[k]);
}
}

for(i=0; i<resultingGroups.length; i++){
document.write("<br>"+"resultingGroup_"+i);
var myGroup = resultingGroups[i];
for(k=0; k<myGroup.length; k++){
document.write("<br>"+myGroup[k]);
}
}
//-->
</script>
</body>
</html>[/HTML]
Nov 4 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by ubccis | last post: by
2 posts views Thread by Terry | last post: by
9 posts views Thread by Charles Banas | last post: by
5 posts views Thread by whisper | last post: by
3 posts views Thread by matthewburton | last post: by
21 posts views Thread by utab | last post: by
9 posts views Thread by barmy_mad | last post: by
5 posts views Thread by asdf | last post: by
2 posts views Thread by oopsatwork | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.