I have an array with two dimensions which I'm looping through to create a table. Example:
Let's say I have array[y][x]
. array[0][x]
will populate the header column, array[1][x]
will create the second column and so on. y's represent each category/row. It's a little confusing so here's a diagram:
[0][0]Device | [1][0] Device 1 | [2][0] Device 3 | [3][0] Device 1(Duplicate)
--------------------------------------------------------------------------------
[0][1]category1 | [1][1] | [2][1] | [3][1]
--------------------------------------------------------------------------------
[0][2]Category2 | [1][2] | [2][2] | [3][2]
I have to check for duplicates in each column at [y][1]
and if there is a duplicate at array[y][1]
, I don't want to print that column at all.
I'm printing each column and row with two nested for loops.
// for every named field, generate a row for each disk
//x represents each table category, which is in rows
for (var x = 0; x < array[0].length; x++) {
// code to print table with field_names[0][x]
for (var y = 1; y < (array.length); y++) {
new_row += "<td>" + [array[y][x]];
}
}
My current thought is for each time the process gets to array[y][ 1 ] I need to check if it matches the value for previous columns [y-1][ 1 ],[y-2][ 1 ],[y-3][ 1 ] and so on because I only want to print the first instance of the table column. So I am basically looking for a way to make a for or a while loop out of that that doesn't go on a printing frenzy and print all my tables multiple times. In pseudo-code I need something that does:
if array[y][1] == array[i][1] where (i = (y - 1); i > 0; i--)
, then don't print the column.
The only problem is every time I try to define i, it effects the rest of the code to where I am printing a bunch of times because everything is so nested. Please help! and Thank you so much.