javaScript刪除對象、數組中的null、undefined、空對象、空數組方法


這兩天在項目中遇到后台需要傳的數據為不能有null,不能有空值,而這個數據又是一個龐大的對象,對組集合,所以寫了個方法來解決這個問題。為了兼具所有的種類類型,封裝了方法,代碼如下:

let obj = {
        a: {
            a_1: 'qwe',
            a_2: undefined,
            a_3: function (a, b) {
                return a + b;
            },
            a_4: {
                a_4_1: 'qwe',
                a_4_2: undefined,
                a_4_3: function (a, b) {
                    return a + b;
                },
                a_4_4: {
                    a_4_4_1: undefined,
                    a_4_4_2: undefined,
                    a_4_4_3: undefined,
                    a_4_4_4: {
                        a_4_4_4_1: undefined,
                        a_4_4_4_2: undefined,
                        a_4_4_4_3: undefined,
                        a_4_4_4_4: undefined,
                        a_4_4_4_5: undefined,
                        a_4_4_4_6: undefined
                    }
                }
            }
        },
        b: [{
            a_1: 'qwe',
            a_2: undefined,
            a_3: function (a, b) {
                return a + b;
            },
            a_4: {
                a_4_1: 'qwe',
                a_4_2: undefined,
                a_4_3: function (a, b) {
                    return a + b;
                },
                a_4_4: {
                    a_4_4_1: undefined,
                    a_4_4_2: undefined,
                    a_4_4_3: undefined,
                    a_4_4_4: {
                        a_4_4_4_1: undefined,
                        a_4_4_4_2: undefined,
                        a_4_4_4_3: undefined,
                        a_4_4_4_4: undefined,
                        a_4_4_4_5: undefined,
                        a_4_4_4_6: undefined
                    }
                }
            }
        }],
        c: [{
            a: undefined,
            b: undefined,
            c: undefined,
            d: undefined
        }, {
            a: undefined,
            b: undefined,
            c: undefined,
            d: undefined
        }]
    };

以下是javaScript部分: 

//判斷對象是否沒有屬性,如{}或者[]
    function isEmptyObj(o) { for (let attr in o) return !1; return !0 }
    function processArray(arr) {
        for (let i = arr.length - 1; i >= 0; i--) {
            if (arr[i] === null || arr[i] === undefined) arr.splice(i, 1);
            else if (typeof arr[i] == 'object') removeNullItem(arr[i], arr, i);
        }
        return arr.length == 0
    }
    function proccessObject(o) {
        for (let attr in o) {
            if (o[attr] === null || o[attr] === undefined) delete o[attr];
            else if(typeof o[attr]=='object') {
                removeNullItem(o[attr]);
                if (isEmptyObj(o[attr])) delete o[attr];
            }
        }
    }
    function removeNullItem(o,arr,i) {
        let s = ({}).toString.call(o);
        if (s == '[object Array]') {
            if (processArray(o) === true) {//o也是數組,並且刪除完子項,從所屬數組中刪除
                if (arr) arr.splice(i, 1);
            }
        }
        else if (s == '[object Object]') {
            proccessObject(o);
            if (arr&&isEmptyObj(o)) arr.splice(i, 1);
        }
    }
    removeNullItem(obj)
    console.log(obj)

 

如果只處理對象null,undefined項,不移除數組中undefined,null的項,保持數組長度則去掉removeNullItem,processArray刪除數項即可,測試數據在上面示例中

-收縮JavaScript代碼
     function processArray(arr) {
        for (let i = arr.length - 1; i >= 0; i--) {
            /*if (arr[i] === null || arr[i] === undefined) arr.splice(i, 1);
            else */if (typeof arr[i] == 'object') removeNullItem(arr[i], arr, i);
        }
        return arr.length == 0
    }
    function removeNullItem(o,arr,i) {
        let s = ({}).toString.call(o);
        if (s == '[object Array]') {
            if (processArray(o) === true) {//o也是數組,並且刪除完子項,從所屬數組中刪除
                //if (arr) arr.splice(i, 1);
            }
        }
        else if (s == '[object Object]') {
            proccessObject(o);
            //if (arr&&isEmptyObj(o)) arr.splice(i, 1);
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM