如何在数组中动态传递列值以及如何通过数组索引设置值

时间:2019-12-04 08:09:18

标签: jquery

我试图在数组中动态传递列名以替换如果不退出数据属于特定索引的值。

这是我的代码:

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>';
                }
            }

        }
    }
  

结果期望:所有空值(未定义)应替换为   固定文字(“不能为空”)

0 个答案:

没有答案
相关问题