Object empty value key filter
过滤空值
Utils
emptykeysFilter()
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2018.11.23
* @modified 2018.11.23
*
* @description Utils 过滤空值
* @augments
* @example emptykeysFilter(obj);
*
*/
const emptykeysFilter = (obj = {}, debug = false) => {
// const newObj = {};
let keys = Object.keys(obj);
keys.forEach(
(key, i) => {
// console.log(`key`, key);
// console.log(`obj[key]`, obj[key]);
if (typeof (obj[key]) === "boolean" || typeof (obj[key]) === "number") {
// do nothing
} else if (!Object.keys(obj[key]).length) {
delete obj[key];
} else {
// newObj[key] = obj[key];
}
}
);
// console.log(`new obj =`, JSON.stringify(newObj, null, 4));
// return newObj;
// console.log(`new obj =`, JSON.stringify(obj, null, 4));
return obj;
};
const Utils = {
emptykeysFilter,
};
export default Utils;
export {
Utils,
emptykeysFilter,
};
/*
let obj = {
"id": "123",
"name": [],
"userName": {},
"telephoneNum": [1],
"phoneNum": {k: "v"},
"email": "",
"bool": false,
"boolean": true,
"no": 666,
};
// Object.keys(obj.bool).length
emptykeysFilter(obj);
// obj = emptykeysFilter(obj);
// {
// "id": "123",
// "telephoneNum": [1],
// "phoneNum": {k: "v"},
// "bool": false,
// "boolean": true,
// "no": 666,
// };
JSON.stringify(obj, null, 4);
*/