js删除 object中的空值

var data = {

  a: 'a',

     b: ''

}

 

删除 b和''的配对,

 

/**
 * Delete all null (or undefined) properties from an object.
 * Set 'recurse' to true if you also want to delete properties in nested objects.
 */
function delete_null_properties(test, recurse) {
    for (var i in test) {
        if (test[i] === null) {
            delete test[i];
        } else if (recurse && typeof test[i] === 'object') {
            delete_null_properties(test[i], recurse);
        }
    }
}

 reference: http://stackoverflow.com/questions/286141/remove-blank-attributes-from-an-object-in-javascript

posted on 2016-10-29 21:08  old土哥  阅读(1884)  评论(0编辑  收藏  举报

导航