js 清除json 中的空节点.

一个递归函数解决了 将空节点和空值对象的节点从json中清除

伸手拿代码 给个赞

 1 function trim_nulls(data) {
 2     var y;
 3     for (var x in data) {
 4         y = data[x];
 5         if (y instanceof Object) y = trim_nulls(y);
 6         if (y === "null" || y === null || y === "" || typeof y === "undefined" || (y instanceof Object && Object.keys(y).length == 0)) {
 7             delete data[x];
 8         }
 9     }
10     return data;
11 }

 

posted @ 2013-05-16 15:22  TonyGuo  阅读(1900)  评论(1)    收藏  举报