1

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.

Jaak Kütt
  • 2,566
  • 4
  • 31
  • 39
DwightB
  • 41
  • 3
  • Create an object for each row whose properties are the values that have been seen before. Then check whether the property exists before displaying the column. – Barmar Feb 25 '13 at 18:52
  • You should transpose the array to a row-based one. That makes it more memory-efficient, and you can easily [remove duplicates](http://stackoverflow.com/q/1960473/1048572) from it – Bergi Feb 25 '13 at 19:57

2 Answers2

1

You wrote out the answer in your thoughts paragraph. Check if it matches if it does then don't print.

for(var y = 1; y< array.length; y++){
    for(var x = 0; x < array[0].length; x++){
        for(var i = array[0].length; ; i--){
            if(array[y][i] == array[y][x]){
                //don't print!
            }else{    
                //print!
            }
        }
    }
}

This is not the best as far as performance goes but it will make sure you don't print any duplicates. I would look into doing what @barmar suggested which is creating an object for each row. But if you don't want to then you should be able to implement the above to do what you want.

Ryan
  • 5,644
  • 3
  • 38
  • 66
0

Before printing, iterate over each row keeping record of the values that already appears for the first time. Every time you see that this record is not new, do not print it.

var found, aux;  
for(var y = 1; y < array.length; y++) {

    aux = {};

    for(var x = 0; x < array[0].length; x++) {

       found = false;

       for (var i = 0; i < aux.length; i++) {
           if (aux[i] == array[y][x]) { found = true; break; }
       }

       if (!found) {
          aux.push(array[y][x]);
          //render
       }

    }
}

Not that efficient, but...

Hope I helped.

Walter Macambira
  • 2,574
  • 19
  • 28