// 前端向后台提交数据的时候避免不了属性对应值为空{a: '',b:'123'}
//处理完成为{b:'123'}
// 或者属性对应数组及数组里有空的字符串, {a: [' ','123']}
//处理完成为{a: ['123']}
// 或者属性对应数组数组数组内有好多对象.其中对象下含有空字符串 {value: [{a: '',b:'123'}]}
// 处理完成为 {value: [{b:'123'}]}
export function deleteEmptyObject(object) {
for (var i in object) {
var value = object[i];
const type = typeof value;
// 去除空格
if (type === 'string') {
object[i] = trim(value);
}
if (typeof value === 'object') {
// 是否引用数据类型
if (Array.isArray(value)) {
if (value.length === 0) {
delete object[i];
continue;
}
}
deleteEmptyObject(value);
if (isEmpty(value)) {
delete object[i];
}
} else {
// 基本数据类型
if (value === '' || value === null || value === undefined) {
delete object[i];
}
}
}
return object;
}
/**
* @param {Object} obj
* @returns {Object}
*/
export function trim(str) { // 删除左右两端的空格
return str.replace(/(^\s*)|(\s*$)/g, '');
}
// 判断对象是否为空
export function isEmpty(object) {
for (var name in object) {
return false;
}
return true;
}