Advertisement
SwVitaliy

Untitled

May 31st, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Example:
  2. //   var a = {a:[],b:[{o:{c:[],d:1,e:[],f:"",},g:null,h:true,},{i:false,j:function(){}}]};
  3. //   emptyArrayToNull(a);
  4.  
  5. function emptyArrayToNull(obj) {
  6.   if (typeof obj === 'object' && obj !== null) {
  7.       if (obj.constructor.name === 'Array') {
  8.         if (obj.length === 0) {
  9.           return null;
  10.         }
  11.         for (var i = 0; i < obj.length; i++) {
  12.           obj[i] = emptyArrayToNull(obj[i]);
  13.         }
  14.         return obj;
  15.       }
  16.  
  17.       for (var i in obj) {
  18.         if (obj.hasOwnProperty(i)) {
  19.           obj[i] = emptyArrayToNull(obj[i]);
  20.         }
  21.       }
  22.     }
  23.  
  24.     return obj;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement