1

I'm using jquery each loop to loop through rows in the table, if there is a row that already exists with the attribute data-prd-id then i will hide it, I successfully did it however, as I know the for loop run much faster than Jquery.each. I would like to change, but I haven't had any idea of how to iterate through rows with for loop.

Here is my code of Jquery.each

var duplicateArr= {};

    $('#table-id > tbody > tr.tr1').each(function () {
        var txt = $(this).attr('data-prd-id');
        if (duplicateArr[txt] && txt != 0)
            $(this).hide();
        else
            duplicateArr[txt] = true;
    });

What i tried so far with for loop but it didn't work:

var duplicateArr= {};
    var objTest = $('#table-id > tbody > tr.tr1');
    for (var i = 0; i < objTest.length; i++) {
        var txt = objTest.rows[i].attr('data-prd-id');
        if (duplicateArr[txt] && txt != 0)
            objTest.rows[i].hide();
        else
            duplicateArr[txt] = true;
    }
  • See this question for more details and examples : http://stackoverflow.com/questions/11887450/each-vs-for-loop-and-performance. The question is : how many rows are you processing ? What will be the performance improvement ? Will you significantly increase user experience ? What will you loose in code readability ? – Florian Motteau Jul 02 '15 at 08:07

1 Answers1

2

You could try this

var trList = $("#tableId > tbody ").children("tr");

for(var i =0; i<trList.length;i++){
    console.log($(trList[i]));
}
Raja
  • 851
  • 9
  • 22