Skip to main content
dublove
Legend
January 4, 2025
Answered

My basic logic is confused: how to compare the row 1 with row2.

  • January 4, 2025
  • 2 replies
  • 692 views
I want to compare the contents of row 1 and row 2, and if the contents are same(same row),  delete the second row.

It's a bit confusing to use for and if together.
logical disorder.
It always feels like the loop nesting isn't finished.
Or it just starts and ends.

 

 

for(var i = 0; i < table.columns.cells.length; i++){
	if(table.rows[0].cells[i].contents==table.rows[1].cell[i].contents){
	table.rows[0].remove();
	}
	else{
	}
	}

 

 

 
Correct answer m1b

Hi @dublove, you can use the "everyItem" method to quickly collect the contents of every cell of the row. Like this:

if (table.rows[0].cells.everyItem().contents.join() == table.rows[1].cells.everyItem().contents.join())
    table.rows[1].remove();

.

2 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
January 4, 2025

Hi @dublove, you can use the "everyItem" method to quickly collect the contents of every cell of the row. Like this:

if (table.rows[0].cells.everyItem().contents.join() == table.rows[1].cells.everyItem().contents.join())
    table.rows[1].remove();

.

dublove
dubloveAuthor
Legend
January 5, 2025

Hi m1b.

extremely grateful
You've got this one, simple and working. It's very efficient.

But I still want to know why this one of mine is stuck.
It seems to go into a dead loop.
It took me all night to figure it out.

var sameRow=false;
for(var i = 0; i < table.columns.length; i++){
	if(table.rows[0].cells[i].contents==table.rows[1].cell[i].contents){
		sameRow=true;
	}
	else{
		sameRow=false;
		break;
	}
	}
	if( sameRow==true){
		table.rows[0].remove();
	}

 

m1b
Community Expert
Community Expert
January 5, 2025

There was a couple of mistakes ... This line:

if(table.rows[0].cells[i].contents==table.rows[1].cell[i].contents){

 

should better be this:

if (table.columns[i].cells[0].contents == table.columns[i].cells[1].contents) {

 

1. You are looping over the columns, not the rows.

2. The column index changes with the loop var i (not the cell index)

 

You could code it something like this:

var sameRow = true;

for (var i = 0; i < table.columns.length; i++) {

    if (table.columns[i].cells[0].contents != table.columns[i].cells[1].contents) {
        sameRow = false;
        break;
    }

}

if (sameRow == true) {
    table.rows[0].remove();
}

 

This switches "sameRow" to false if any cell doesn't match. This seems easier to me, but there are many many ways to do it.

 

For your extra learning, I would definitely try to make a function of it, too:

// call the function
if (isSameRowContents(table.rows[0], table.rows[1])) {
    // remove the double row
    table.rows[0].remove();
}


/**
 * Returns true when the two rows have matching contents.
 * @9397041 {Row} row1 - a table row.
 * @9397041 {Row} row2 - a table row.
 * @Returns {Boolean}
 */
function isSameRowContents(row1, row2) {

    var cellCount = Math.min(row1.cells.length, row2.cells.length);

    for (var i = 0; i < cellCount; i++) {
        if (row1.cells[i].contents != row2.cells[i].contents) {
            return false;
        }
    }

    return true;

};

 

Don't be discouraged, I am pleased that you are trying so hard. Keep it up!

- Mark

Robert at ID-Tasker
Legend
January 4, 2025

I'm sorry, but what are you doing? 

 

It's like you're constantly ignoring all the previous information / advice - and example code - and are putting some garbled / random code?

 

This doesn't make any sense: table.columns.cells.length

 

columns collection doesn't have cells - so you should iterate through the collection of columns of the table: 

 

table.columns.length

 

 

But then, you don't have guarantee, that number of cells in both rows is the same - some cells can be merged - horizontally or vertically. 

 

dublove
dubloveAuthor
Legend
January 4, 2025

I delete duplicate header rows.
There is a “faux header” that is sometimes deleted.

It's bedtime. Good night.

Robert at ID-Tasker
Legend
January 4, 2025

To avoid problems like this - you should "mark" rows like this - added during 1st processing - in case that they could / should be removed at some point - then remove them first before doing any updates / extra processing. 

 

You can "mark" Table / Column / Row / Cell - and other objects in InDesign - using label property - or you can use insertLabel and then extractLabel to check the value.