0

我有一个包含数组的对象:

<script type="text/javascript">
    var statusData = {
        Status: []
    };

    var uniqueCounter = 1

    function createJsonFmtData() {      

        // Used as unique id at client side 
        var uniqueCounter =uniqueCounter + 1;

        statusData.Status.push({
            "Name": Name,
            "Time": Time,
            "Email": Mail,
            "Name": Type,
            "Value": Value,
            "uniqueId": uniqueCounter
        });
    }

    function DelNewlyCreStatusRow(rowId) {
        // First pop elment from json data
        var val;
        for (val = 0; val < statusData.Status.length; ++val) {
            if (statusData.Status[val].uniqueId == rowId) {
                delete statusData.Status[val];
                break;
            }
        }
</script>

当尝试调用DelNewlyCreStatusRow它时会出现错误:

类型错误:statusData.Status[val] 未定义

我无法在这里弄清楚,因为我已将其声明为全球性的。

4

4 回答 4

2

这是因为您试图从数组中错误地删除。delete运算符在数组上很有趣。它将元素替换为undefined. 看一下这个:

>>> var A = [1, 2, 3];
>>> delete a[1];
>>> A;
[1, undefined, 3];

DelNewlyCreStatusRow因此多次调用将引发异常,因为statusData.Status[val].uniqueId无法评估(statusData.Status[val]is undefined)。

要解决此问题,请使用此代码而不是delete

var val;
for (val = 0; val < statusData.Status.length; ++val) {
    if (statusData.Status[val].uniqueId == rowId) {
        statusData.Status.splice( val, 1 );
        break;
    }
}

请注意,它splice修改了一个数组,因此如果您想一次进行多次删除,那么您必须将for循环替换为while( 并稍微重构代码 )。break由于声明,这里不需要。

于 2012-11-27T13:00:03.887 回答
1

你应该更换

delete statusData.Status[val];

statusData.Status.splice(val,1);
val -= 1;

删除数组中的对象。

于 2012-11-27T12:59:56.687 回答
0

您在函数内的第二个 var 声明中在代码中犯了错误。var uniqueCounter = uniqueCounter + 1 => NAN + 1。第二次不需要 var,所以它只是 uniqueCounter = uniqueCounter + 1 => 2。

然后在我的案例中删除工作正常。

<script type="text/javascript">

        var statusData = {
            Status : []
        };

        var uniqueCounter = 1

        function createJsonFmtData() {

            var statusName = $('#<%= ddlStatus.ClientID %> option:selected').text();
            var dateTime = $("#body_ctl04_ctl02").val();
            var forceMail = ($("#chkForceMail").is(':checked')) ? 1 : 0;
            var noteType = $('#divTrackId').text();
            var dataValue = $("#<%=txtTrackId.ClientID %>").val();
            var curLogedUserName = $("#<%=curLoginUserName.ClientID %>").val();

            // Used as unique id at client side
            uniqueCounter = uniqueCounter + 1;

            statusData.Status.push({

                "statusName" : statusName,
                "dateTime" : dateTime,
                "forceEmail" : forceMail,
                "typeName" : noteType,
                "dataValue" : dataValue,
                "uniqueId" : uniqueCounter
            });
        }

        function DelNewlyCreStatusRow(rowId) {

            // First pop elment from json data
            var val;
            for ( val = 0; val < statusData.Status.length; ++val) {
                if (statusData.Status[val].uniqueId == rowId) {
                    console.log(typeof(statusData.Status[val]));
                    delete statusData.Status[val];
                    break;
                }
            }
        }
        createJsonFmtData();
        console.log(statusData);
        DelNewlyCreStatusRow(2);
        console.log(statusData);
    </script>
于 2012-11-27T13:12:33.747 回答
0

函数 DelNewlyCreStatusRow 缺少结束 '}'

function DelNewlyCreStatusRow(rowId) {
    // First pop elment from json data
    var val;
    for (val = 0; val < statusData.Status.length; ++val) {
        if (statusData.Status[val].uniqueId == rowId) {
            delete statusData.Status[val];
            break;
        }
    }
}
于 2012-11-27T13:04:53.280 回答