Refresh JSON after delete-Collection of common programming errors

I have got following JSON:

 "memberValidations":
       [
           {
               "field": "PRIMARY_EMAIL",
               "errorCode": "com.endeavour.data.validation.PRIMARY_EMAIL",
               "createdDateTime": null
            },
            {
               "field": "lastName",
               "errorCode": "com.endeavour.data.validation.lastName",
               "createdDateTime": null
             },
       ]

After deleting an object delete memberValidations[0];

I’m expecting lastname from memberValidations[0]; but getting undefined instead? Any idea how to fix this?

  1. Using delete will remove the item from the array but it won’t resize it so effectively you still have an item at index 0 but it’s no longer assigned (undefined). From the docs

    When you delete an array element, the array length is not affected. This holds even if you delete the last element of the array.

    To remove the item completely you need to also resize the array e.g.

    memberValidations.splice(0, 1)
    

Originally posted 2013-11-09 23:22:37.