Remove a JSON attribute

This question already has an answer here:

  • How do I remove a property from a JavaScript object? 33 answers

  • 简单:

    delete myObj.test.key1;
    

    The selected answer would work for as long as you know the key itself that you want to delete but if it should be truly dynamic you would need to use the [] notation instead of the dot notation.

    For example:

    var keyToDelete = "key1";
    var myObj = {"test": {"key1": "value", "key2": "value"}}
    
    //that will not work.
    delete myObj.test.keyToDelete 
    

    instead you would need to use:

    delete myObj.test[keyToDelete];
    

    Substitute the dot notation with [] notation for those values that you want evaluated before being deleted.

    链接地址: http://www.djcxy.com/p/4664.html

    上一篇: 如何删除/取消设置JavaScript对象的属性?

    下一篇: 删除JSON属性