0

I have an object containing an array:

<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>

When try to call DelNewlyCreStatusRow it gives the error:

TypeError: statusData.Status[val] is undefined

I am not able to figure it out here where as I have declared it as global.

1
  • It shouldn't be undefined. Here's a fiddle, corrected a few little errors (can't remember which), seems to be working as expected: jsfiddle.net/SJB6c/1 - and yes, you're making a whole in the array, not removing and pulling the other elements back - as it'd happen with splice. You mentioned nothing about that, so I left it. Commented Nov 27, 2012 at 13:14

4 Answers 4

2

This is because you are trying to delete from the array incorrectly. delete operator is quite funny on arrays. It replaces element with undefined. Check this out:

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

Thus calling DelNewlyCreStatusRow multiple times will throw an exception, because statusData.Status[val].uniqueId cannot be evaluated ( statusData.Status[val] is undefined ).

To fix this use this code instead of delete:

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

Note that splice modifies an array, so if you want to do multiple deletes in one go, then you will have to replace for loop with while ( and refactor the code a bit ). This is not needed here because of break statement.

Sign up to request clarification or add additional context in comments.

Comments

1

You should replace

delete statusData.Status[val];

with

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

to remove an object in an array.

Comments

0

The function DelNewlyCreStatusRow missing a closing '}'

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

Comments

0

You made an Error in your Code at the second var declaration inside the Function. var uniqueCounter = uniqueCounter + 1 => NAN + 1. You dont need the var a Second time so it's just uniqueCounter = uniqueCounter + 1 => 2.

Then delete works fine in my Case.

<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.