列总NAN错误

Kar*_*hik 1 jquery nan

我正在使用这段代码将值添加到表列中,该列非常有效,直到遇到带有值的null td单元格.从循环中的那一点开始,我在警报中收到NaN错误.我想知道如何忽略这些非数字值或用零替换它们进行计算?

jQuery(function() {
        var MarketCapTotal = 0;
        // loop through the table
        jQuery('#grdWatchlistname tbody tr').each(function() {
        // replace the dollar signs and commas
        var MarketCap = (jQuery('td:nth-child(4)', jQuery(this)).html
().replace('$', '').replace(/[^a-zA-Z 0-9]+/g, ''));
            var td4th = jQuery('td:nth-child(4)', jQuery(this));
            MarketCapTotal += parseInt(MarketCap);
            alert(MarketCapTotal);
        });
    }); 
Run Code Online (Sandbox Code Playgroud)

Rub*_*ias 5

试试这个:

MarketCapTotal += isNaN(MarketCap) ? 0: parseInt(MarketCap, 10);
Run Code Online (Sandbox Code Playgroud)