SoFunction
Updated on 2025-03-02

Introduction to removing empty strings and changing key/value

Operation for JSON strings. Remove key values ​​and add attributes.

//Delete the value of the JSON objectvar json=[.....];
delete(json['key']);
or
delete();
//Add object object=value;
or
json['object']=value;

If the data is obtained by querying the database, there may be null values, and removing null values ​​one by one for loop JSON data or operating data is more cumbersome.

At this time, you can use for…in to loop the attribute to remove null values ​​or manipulate data.

 function removeEmptyObject(object){
   for (var i in object) {
  var value = object[i];
  if (typeof value === 'object') {
   if ((value)) {
    if ( == 0) {
     delete object[i];
     continue;
    }
   }
   removeEmptyObject(value);
   //Add on demand   if (isEmpty(value)) {
    delete object[i];
   }
  } else {
   if (value === '' || value === null || value === undefined) {
    delete object[i];
   } else {
   }
  }
 }
 return object;
}
//Is it emptyfunction isEmpty(object) {
 for (var name in object) {
  return false;
 }
 return true;
}

Operate the organizational structure JSON data, remove the empty key value, and merge the subset contents into the new key children.

 function removeEmptyObject(object){
   for (var i in object) {
    var value = object[i];
    if (typeof value === 'object') {
     if ((value)) {
      if ( == 0) {
        //alert(object[i]);
       delete object[i];
       continue;
      }else{ 
        var a=[];
        if(i=='jobChildren'){
          for (var j = 0; j < object[i].length; j++) {
            (object[i][j]);
          }
          delete object[i];
        }else if(i=='userChildren'){
          for (var j = 0; j < object[i].length; j++) {
            (object[i][j]);
          }
          delete object[i]; 
        }else if(i=='deptChildren'){
          for (var j = 0; j < object[i].length; j++) {
            (object[i][j]);
          }
          delete object[i]; 
        }else if(i=='companyChildren'){
          for (var j = 0; j < object[i].length; j++) {
            (object[i][j]);
          }
          delete object[i]; 
        }else if(i=='jobDeptChildren'){
          for (var j = 0; j < object[i].length; j++) {
            (object[i][j]);
          }
          delete object[i];
        }
        if(>0&&object["children"]!=undefined){
          for (var k = 0; k < object["children"].length; k++) {
            (object["children"][k]);
          }
          object["children"]=a;
        }else if(>0 && object["children"]==undefined){
          object["children"]=a;
        } 
      }
     }
     removeEmptyObject(value);
     if (isEmpty(value)) {
      delete object[i];
     }
    } else {
     if (value === '' || value === null || value === undefined) {
      delete object[i];
     } else {
     }
    }
   }
  }
  function isEmpty(object) {
   for (var name in object) {
    return false;
   }
   return true;
  }

If you need it, try it.

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links