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


    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);
    
    
    */
    
    
    
  • 相关阅读:
    silverlight的TranslateTransform 的使用
    720 JavaScript函数的this指向
    JavaScript数组
    JavaScriptDOM事件
    JavaScript流程控制语句
    CSS布局案例
    JavaScriptDOM基础
    JavaScriptDOM事件
    JavaScript基础语法
    JavaScript的String对象相关方法
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/10006273.html
Copyright © 2020-2023  润新知