当前位置: 移动技术网 > IT编程>开发语言>JavaScript > javaScript删除对象、数组中的null、undefined、空对象、空数组方法

javaScript删除对象、数组中的null、undefined、空对象、空数组方法

2018年11月14日  | 移动技术网IT编程  | 我要评论

这两天在项目中遇到后台需要传的数据为不能有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);
        }
    }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网