xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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;
    }

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, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(195)  评论(3编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2015-11-23 TechNet 技术资源库 windows for all things !
2015-11-23 Seven xxx in Seven Weeks ebooks All In One
2015-11-23 HHVM的全称是"HipHop for PHP",开放源代码。采用PHP许可证授权!
2015-11-23 Clojure
2015-11-23 ASP.Net MVP Framework had been dead !
2015-11-23 hosts文件
2015-11-23 Browser Showdown – Microsoft Edge vs Chrome, Firefox, and IE All In One
点击右上角即可分享
微信分享提示