码迷,mamicode.com
首页 > Web开发 > 详细

js删除 object中的空值

时间:2016-10-29 22:18:39      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:pretty   delete   attr   str   int   undefined   flow   cts   ack   

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

js删除 object中的空值

标签:pretty   delete   attr   str   int   undefined   flow   cts   ack   

原文地址:http://www.cnblogs.com/ifykwf/p/6011814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!