发布于 5年前

js 过滤空对象、空字符串

/**
 * 过滤空对象
 */
const deleteEmptyObject = (obj, recurse) => {

  for (var i in obj) {
    if (isEmptyObj(obj[i]) ) {
        delete obj[i];
    } else if (recurse && typeof obj[i] === 'object') {
        deleteEmptyObject(obj[i], recurse);
    }
  }
  return obj;
}
/**
 * 过滤空字符串
 */
const deleteEmptyString = (obj, recurse) => {
  for (var i in obj) {
      if (obj[i] === '' ) {
          delete obj[i];
      } else if (recurse && typeof obj[i] === 'object') {
          deleteEmptyString(obj[i], recurse);
      }
  }
  return obj
}
©2020 edoou.com   京ICP备16001874号-3