• 数组中子元素为空数组时去掉为空的数组


    数组中子元素为空数组时去掉为空的数组使用过滤的方式即可:

    var arrayOut2 = [
            [{name:'张无忌',age:28},{name:'赵敏',age:24}],
            [],
            [{name:'宋青书',age:28},{name:'周芷若',age:26}],
            [{name:'成昆',age:48}],
            []
        ]
        var res = arrayOut2.filter(item => {
            return item.length > 0
        });
        console.log(res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    得到的结果是三个子元素:在这里插入图片描述

    那么当数据更为复杂的时候,比如数组A中包含对象A,对象A中包含below数组B,below数组B又包含对象B,对象B又包含below空数组,当对象B的below数组为空时,删掉整个对象B。

    数据例子如下展示:
    其中arrayOut是数组Aname: '五比’的是对象A,需要删掉整个类似**{name: ‘比价格’,below:[]},**的数据

     var arrayOut= [
           {name: '五比',below:[
                {name: '比高度',below:[
                    {content: '比跳高高度'},
                    {content: '比站立高度'},
                    {content: '比摸高高度'},
                ]},
                {name: '比质量',below:[
                    {content: '比质量1'},
                    {content: '比质量2'},
                    {content: '比质量3'},
                ]},
                {name: '比价格',below:[]},
                {name: '比服务',below:[
                    {content: '比服务1'},
                    {content: '比服务2'},
                    {content: '比服务3'},
                ]},
                {name: '比服务',below:[]},
            ]},
            {name: '评测',below:[
                {name: '测品质',below:[]},
                {name: '测数量',below:[
                    {content: '测数量1'},
                    {content: '测数量2'},
                    {content: '测数量3'},
                ]},
                {name: '测指数',below:[]},
                {name: '测有害物质',below:[]},
                {name: '测合格率',below:[
                    {content: '测合格率1'},
                    {content: '测合格率2'},
                    {content: '测合格率3'},
                ]},
            ]},
        ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    那么现在使用如下代码即可达到对应效果:

    arrayOut.forEach((item) => {
            console.log(item)
            var idxArr = []
            item.below.forEach((it, idx) => {
                var name = it.below.length <= 0 ? it.name : ''
                if (name !== '') {
                    idxArr.unshift(idx)
                }
            })
            var number = idxArr.length
            for (var i = 0; i < number; i++) {
                console.log('idxarr[i]',idxArr[i])
                item.below.splice(idxArr[i], 1)
            }
        })
        console.log(arrayOut)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    此处使用unshift是因为把数组中排序靠后的先删除,便不会影响后续的删除,如果从下标较小的开始删除。可能会出现不能完全删掉空数组对应的数据。
    从打印结果来看,由较大的下标开始删除:
    在这里插入图片描述
    最后处理得到的数据如下,分别是3条和2条:
    在这里插入图片描述

  • 相关阅读:
    频繁设置CGroup触发linux内核bug导致CGroup running task不调度
    打家劫舍 III
    pwnable_hacknote
    wayland 概述
    计算机视觉与深度学习 | 视频/图像转换及保存播放(Matlab源码)
    IDEA本机连接远程TDengine不成功,终于配置成功
    Java - static 关键字
    什么是重放攻击(Reply attack)?
    IP地址、子网掩码、默认网关介绍及例题计算
    Failed to prepare the device for development
  • 原文地址:https://blog.csdn.net/Red_sevenWord/article/details/125633118