我试图在数组中动态传递列名以替换如果不退出数据属于特定索引的值。
这是我的代码:
var excelRows=[{"DWSDYear": 2019, "DWSDDepartment": "IT Department", "DWSDHeadCount": 12},
{"DWSDMonth": "March", "DWSDDepartment": "Sales Department", "DWSDSubDepartment": "Sales", "DWSDTTR": "50000"},
{"DWSDYear": "2021", "DWSDMonth": "April", "DWSDSubDepartment": "Telecom", "DWSDHeadCount": "14", "DWSDTTR": "60000"}];
var ColumnArray=["DWSDYear","DWSDMonth","DWSDDepartment","DWSDSubDepartment","DWSDHeadCount","DWSDTTR"];
CheckDataValue(ColumnArray,excelRows);
//alert(ColumnArray);
function CheckDataValue(ColumnArray,excelRows){
for(var RowIndex=0;RowIndex<excelRows.length;RowIndex++)
{
for(var ColIndex=0;ColIndex<ColumnArray.length;ColIndex++)
{
if(!excelRows[RowIndex].hasOwnProperty(ColumnArray[ColIndex]))
{
//Passing column name dynamically: Not working
//excelRows[RowIndex].ColumnArray[ColIndex]='<span style="color:red;">Can\'t be empty</span>';
//Passing column name manually: Working
excelRows[RowIndex].DWSDTTR='<span style="color:red;">Can\'t be empty</span>';
}
}
}
}
结果期望:所有空值(未定义)应替换为 固定文字(“不能为空”)