對象工具類

備註:若是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] = '';
            }, '');
        }
    }
}
相關文章
相關標籤/搜索