Link to home
Start Free TrialLog in
Avatar of mwheeler_fsd
mwheeler_fsd

asked on

jQuery using .each with conditions to build a separate array() result

Dear Experts,

I am iterating through a table with .each. On each iteration, I have a condition. If the condition is met, I want the resulting row to be stored in an array to be returned, otherwise I want to skip the row. The code snippet below shows what I am doing.

Example:

I have 100 rows of the original table being sent to the function, of which, 50 satisfy the "if" condition. The problem is, the return is sending back 50 nulls, and 50 correct elements. I don't understand why I am getting the nulls? I only want the 50 that satisfied the condition.

Please consider the following:

function storePaymentTableValues(starting_payment_number) {

    var tableData = new Array();

    $('#tbl_payment_schedule tr').each(function(row, tr) {
        
        if(Number($(tr).find('td:eq(0)').text()) >= Number(starting_payment_number)) {
            
            tableData[row] = {
                "PaymentNumber"              : $(tr).find('td:eq(0)').text(),
                "PaymentMethodID"            : $(tr).find('td:eq(1)').text(),
                "PaymentDueDate"             : $(tr).find('td:eq(2)').text(),
                "PaymentDueAmount"           : $(tr).find('td:eq(3)').text(),
                "PaymentReceivedDate"        : $(tr).find('td:eq(4)').text(),        
                "PaymentReceivedAmount"      : $(tr).find('td:eq(5)').text(),        
                "PaymentConfirmationID"      : $(tr).find('td:eq(6)').text(),        
                "AmountAllocatedToPrinciple" : $(tr).find('td:eq(7)').text(),        
                "Memo"                       : $(tr).find('td:eq(8)').text(),
            }
        }
    
    }); 

    return tableData;
}

Open in new window


Aside from the nulls being included, this is doing exactly as expected. How/why is the condition being ignored and the "records" that don't qualify getting included as null?

Thank you in advance for you help.

Best,
Mike
Avatar of mwheeler_fsd
mwheeler_fsd

ASKER

Experts:

Here is an actual result where the "starting_payment_number" is 85 (out of 89). Result looks great, but how am I getting all the nulls that should simply be skipped?

There are 84 nulls representing the records I intended to skip, followed by 85 through 89 which I want in the result.

[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,{"PaymentNumber":"85","PaymentMethodID":"-1","PaymentDueDate":"2015-09-28","PaymentDueAmount":"206","PaymentReceivedDate":"null","PaymentReceivedAmount":"null","PaymentConfirmationID":"null","AmountAllocatedToPrinciple":"null","Memo":"null"},{"PaymentNumber":"86","PaymentMethodID":"-1","PaymentDueDate":"2015-09-29","PaymentDueAmount":"206","PaymentReceivedDate":"null","PaymentReceivedAmount":"null","PaymentConfirmationID":"null","AmountAllocatedToPrinciple":"null","Memo":"null"},{"PaymentNumber":"87","PaymentMethodID":"-1","PaymentDueDate":"2015-09-30","PaymentDueAmount":"206","PaymentReceivedDate":"null","PaymentReceivedAmount":"null","PaymentConfirmationID":"null","AmountAllocatedToPrinciple":"null","Memo":"null"},{"PaymentNumber":"88","PaymentMethodID":"-1","PaymentDueDate":"2015-10-01","PaymentDueAmount":"206","PaymentReceivedDate":"null","PaymentReceivedAmount":"null","PaymentConfirmationID":"null","AmountAllocatedToPrinciple":"null","Memo":"null"},{"PaymentNumber":"89","PaymentMethodID":"-1","PaymentDueDate":"2015-10-02","PaymentDueAmount":"72","PaymentReceivedDate":"null","PaymentReceivedAmount":"null","PaymentConfirmationID":"null","AmountAllocatedToPrinciple":"null","Memo":"null"}]

Thanks again,
--Mike
SOLUTION
Avatar of Hammadh Abdul Rahman
Hammadh Abdul Rahman
Flag of Maldives image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear Experts:

This is always a tough situation, but I feel compelled to award points to both solutions. Hammadh, your solution worked perfectly, and makes sense, now that I have the understanding of how JavaScript handles these arrays based on Julian's explanation, and background foundation.

With that said, I thank both of you. Not only did this solve for my immediate problem, but also provides excellent information I will use in the future.

Best regards,
--Mike