• 对象工具类


    备注:如果let报错,可以修改为var,尤其是IE浏览器

    /**
     * @author: 苗士军
     * @description 对象工具类
     */
    ObjectUtils = {
        /**
         * @description 对象转参数
         * @param object
         * @return {string}
         */
        objectToParam: function (object) {
            let param = "";
            for (let key in object) {
                if (param == "") {
                    param = param + key + "=" + object[key];
                }
                else {
                    param = param + "&" + key + "=" + object[key];
                }
            }
            return param;
        },
        /**
         * @description 复制对象
         * @param: object
         * @return: 复制后对象
         */
        copyObject: function (object) {
            return JSON.parse(JSON.stringify(object));
        },
        /**
         * @description 将对象属性赋值给模板{{}}
         * @param template
         * @param object
         * @return 模板后数据
         */
        renderTemplate: function (template, object) {
            return template.replace(/{{(.+?)}}/g, function (m, m1) {
                return object[m1]
            })
        },
        /**
         * @description 判断对象是否为空
         * @param object
         * @return {boolean}
         */
        isEmpty: function (object) {
            return JSON.stringify(object) == "{}";
        },
        /**
         * @description 获取对象key值集合
         * @param object
         * @return {Array}
         */
        getKeys: function (object) {
            let arr = [];
            if (!this.isEmpty(object)) {
                try {
                    arr = Object.keys(object);
                } catch (e) {
                    for (var key in object) {
                        arr.push(key);
                    }
                }
            }
            return arr;
        },
        /**
         * @description 获取对象value值集合
         * @param object
         * @return {Array}
         */
        getValues: function (object) {
            let arr = [];
            if (!this.isEmpty(object)) {
                try {
                    arr = Object.values(object);
                } catch (e) {
                    for (var key in object) {
                        arr.push(object["" + key]);
                    }
                }
            }
            return arr;
        },
        /**
         * @description 合并对象
         * @param obj1
         * @param obj2 obj2 中的属性会覆盖掉 obj1
         * @return {*}
         */
        assignObject: function (obj1, obj2) {
            if (this.isEmpty(obj1) && this.isEmpty(obj2)) {
                return {};
            }
            try {
                Object.assign(obj1, obj2);
            } catch (e) {
                for (var key in obj2) {
                    obj1[key] = obj2[key];
                }
            }
            return obj1;
        },
        /**
         * @description 选择对象中的一个(多个)属性
         * @param object
         * @param keys
         * @return {{}}
         */
        getKeysProperties: function (object, keys) {
            if (this.isEmpty(object)) {
                return {};
            }
            if (!Array.isArray(keys)) {
                keys = [keys];
            }
            let obj = {};
            keys.reduce(function (a, c, i) {
                obj[c] = object[c]
            }, "");
            return obj;
        },
        /**
         * @description 选择对象中的一个(多个)属性
         * @param object
         * @param keys
         * @return {{}}
         */
        deleteKeysProperties: function (object, keys) {
            if (this.isEmpty(object)) {
                return {};
            }
            if (!Array.isArray(keys)) {
                keys = [keys];
            }
            let arr = this.getKeys(object);
            let obj = {};
            arr.filter(a = > keys.indexOf(a) < 0
            ).
            reduce(function (a, c, i) {
                obj[c] = object[c]
            }, "");
            return obj;
        },
        /**
         * @description 获取对象的属性值
         * @param object
         * @param key
         * @param ignoreCase,默认true
         * @return {*}
         */
        getKeyPropertie: function (object, key, ignoreCase) {
            let value = null;
            if (this.isEmpty(object)) {
                return value;
            }
            ignoreCase = ignoreCase == undefined ? true : ignoreCase;
            for (let k in object) {
                if (ignoreCase) {
                    if (k.toString().toUpperCase() == key.toString().toUpperCase()) {
                        value = object[k];
                    }
                }
                else {
                    if (k == key) {
                        value = object[k];
                    }
                }
            }
            return value;
        },
        /**
         * @description 清空对象属性
         * @param object
         * @param keys
         * @return
         */
        clearObject: function (object, keys) {
            if (!this.isEmpty(object)) {
                keys = keys == undefined ? object.getKeys() : keys;
                if (!Array.isArray(keys)) {
                    keys = [keys];
                }
                keys.reduce(function (a, c, i) {
                    object[c] = '';
                }, '');
            }
        }
    }
  • 相关阅读:
    UVa-10317
    UVa-1595
    UVa-10391
    UVa-10763
    UVa-10935
    UVa-1594
    UVa-1593
    从CSDN搬过来了
    memset会显著增加时间和空间的消耗吗
    memset对数组的初始化
  • 原文地址:https://www.cnblogs.com/miaosj/p/10677275.html
Copyright © 2020-2023  润新知