如何将dafault值传递给NULL或未定义的字段

How to pass dafault values to the NULL or undefined fields?

本文关键字:NULL 未定义 字段 dafault 值传      更新时间:2023-09-26

我有一个小需求,在这里我将动态地向表中获取值。有时,少数字段会返回一些null/blank值。如果它给出空值,显示错误消息,如

"worderId[i].childNodes[0] is undefined"

请帮助我如何为null值分配一些默认值。这里我试了一些代码,

if(worderId.length>0)
{
WOTableData= "<table cellpadding='0' cellspacing='1' border='0' width=100% class='display' id='WOData' ><thead><tr id='row1'><th>&nbsp;</th><th>worderId</th><th>wostatus</th></thead><tbody>";
var technologyImage="";
for(i=0;i<worderId.length;i++)
    {   
        WOTableData=WOTableData+"<tr title='"+worderId[i].childNodes[0].nodeValue+"</td><td>"+ worderId[i].childNodes[0].nodeValue+"</td><td>"+wostatus[i].childNodes[0].nodeValue+"</td></tr>";                                            
    }
WOTableData=WOTableData+"</tbody></table>";

document.getElementById("WODataDiv").innerHTML = WOTableData;
}

谢谢。

您需要在for循环中添加一个简单条件来检查该值。

for(i=0;i<worderId.length;i++)
{   
    if (!worderId[i].childNodes || !worderId[i].childNodes.length || !worderId[i].childNodes[0].nodeValue) {
        worderId[i].childNodes = [{nodeValue: "some_default_value"}];
    }
    WOTableData=WOTableData+"<tr title='"+worderId[i].childNodes[0].nodeValue+"</td><td>"+ worderId[i].childNodes[0].nodeValue+"</td><td>"+wostatus[i].childNodes[0].nodeValue+"</td></tr>";                                            
}