js Object empty value key filter All In One
js Object empty value key filter All In One
对象过滤空值
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);
*/
Object empty Checker
function isEmptyObject(obj) {
return Object.keys(obj).length === 0;
}
const obj = {};
isEmptyObject(obj);
// true
const obj2 = {year: 2023};
isEmptyObject(obj2);
// false
const obj = {};
const trueType = Object.prototype.toString.apply(obj).replace(/\[object /i, ``).replace(/\]/, ``);
Object.prototype.toString.call({}).replace(/\[object /gi, ``).replace(/\]/gi, ``).toLowerCase();
// 'object'
Object.prototype.toString.call({}).replace(/\[object /, ``).replace(/\]/, ``).toLowerCase();
// 'object'
Object.prototype.toString.apply({});
// '[object Object]'
const isEmpty = (obj) => {
if(Object.prototype.toString.call(obj) === '[object Object]') {
return Object.keys(obj).length === 0;
} else {
console.warn(`Your input data is not an object`);
return false;
}
}
isEmpty({});
// true
isEmpty(new Date());
// false
const isObjectEmpty = (objectName) => {
for (let prop in objectName) {
if (objectName.hasOwnProperty(prop)) {
return false;
}
}
return true;
};
const isObjectEmpty = (objectName) => {
return JSON.stringify(objectName) === "{}";
};
function isEmptyObject(value) {
return Object.keys(value).length === 0 && value.constructor === Object;
}
js object empty Checker
https://lodash.com/docs/4.17.15#isEmpty
https://github.com/lodash/lodash/blob/4.17.15/lodash.js#L11479
function isEmpty(value) {
if (value == null) {
return true;
}
if (isArrayLike(value) &&
(isArray(value) || typeof value == 'string' || typeof value.splice == 'function' ||
isBuffer(value) || isTypedArray(value) || isArguments(value))) {
return !value.length;
}
var tag = getTag(value);
if (tag == mapTag || tag == setTag) {
return !value.size;
}
if (isPrototype(value)) {
return !baseKeys(value).length;
}
for (var key in value) {
if (hasOwnProperty.call(value, key)) {
return false;
}
}
return true;
}
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
js data type checker solutions All In One
https://www.cnblogs.com/xgqfrms/p/16198330.html
https://www.freecodecamp.org/news/check-if-an-object-is-empty-in-javascript/
https://www.samanthaming.com/tidbits/94-how-to-check-if-object-is-empty/
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/10006273.html
未经授权禁止转载,违者必究!