Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: problem with nested tables

1. ## problem with nested tables

Hi ,
I would like to dynamically populate tables
var bloc = new Array();which contain nested arrays(with constant size)

bloc[0]= new array(10);
bloc[1]= new array(10);
// till bloc[k]

The T table containing my values are constitued from inner array with different lengths
var T= new Array();
T[0], T[1], T[2],till T[n]

I start to fill the bloc[0] with T[0] and if the length of T[0] is smaller than 10 I go on with T[1]
But I have the constraint to have unique element per bloc
I have already checked than the individual table T[0], T[1], T[n]...have unique elements

for exemple
T[0]= ['1','2','3','4','5','6','20'];
T[1]= ['30','20','10','11'];
then bloc[0]should be['1','2','3','4','5','6','20','30','10','11']

Code:
```function Test(){
var k = 0; //counter of block
var j=0;// loop in  block

var n=0; //counter of nested T
var i=0;// loop in T[]
var found;

bloc[0][0]=T[0][0]; //initialisation: the first element in the first bloc come from the first

elemt of T[0]

for (i=0; i<(T[n].length);i++){  //i is the counter of element in T[]

if(i%10==0)k++; //if already 10 elements in the block then a new bloc created

if (bloc[k][j] == T[n][i]) {
si element found  do nothing
found = true;
//break;
}

if (!found){
bloc[k][bloc[k].length] = T[n][i];
// then put it in the block
}

j++;
}

n++;
}```
The function didnot work
Problem with loop?

• you have problems not with nested tables...u must use nested loops...try again with 2 for loops
1. for the T[0], T[1] ... T[T.length]
-> 2. for the elements insided every T[n]

• Hi shyam
but I don't know how to do it.

I have though to do something like for
for (var i=0;i<(T[0].length);i++){
bloc[0][i] = T[i];
}
but I don't know how many T[n] I have
and how can I go on with the next T[]

• Code:
```for ( i = 0; i < T.length; i++ ) {
for ( j = 0; j < T[i].length; j++ ) {
// T[i][j]
} // inner j
} // outer i```

• thank Shyam for your explanation
Code:
```function Test() {

var k    = 0;

for (var i=0; i<T.length; ++i) {
for (var j=0; j<T[i].length; ++j) {
bloc[k].push(T[i][j]);
if (bloc[k].length == 10) {
++k;
bloc[k] = [];
}
}
}
}```
The function looks better but I still have problem to go to the next bloc after 10 elements

• Now it is working
I have forgotten some '('
Thank for you helps

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•