位置:首页 > web前端 > javascript

 js如何遍历删除多维数组  js遍历删除多维数组的方法

dearweb 发布:2023-03-15 20:01:04阅读:

在日常开发中,我们经常遇到删除数组的某一个值,但是多维数组的情况一般很少遇到,本文给大家介绍一下,多维数组中删除某一个元素的方法。

        let arr = [
            {
                "isRequired": false,
                "componentType": 23,
                "fieldNames": "apply_user_name",
                "icon": "ivu-icon ivu-icon-ios-git-network",
                "selectResValue": "2",
                "uuid": "89897400-af3c-4ade-b84b-dfeada64b989",
                "basePresetTxt": "文本回显框",
                "resultVal": "",
                "selectList": [],
                "actualPayment": 0,
                "fieldTypeNames": "String",
                "checked": false,
                "componentName": "SingleLineDisableCard",
                "baseTitle": "文本回显框",
            },
            {
                "componentType": 15,
                "componentsList": [
                    {
                        "componentsList": [
                            {
                                "isRequired": false,
                                "componentType": 23,
                                "fieldNames": "bumen",
                                "icon": "ivu-icon ivu-icon-ios-git-network",
                                "selectResValue": "1",

                                "uuid": "64bc5718-2e41-4e7d-a4b8-aad6efc629fe",
                                "basePresetTxt": "文本回显框",
                                "resultVal": "",
                                "selectReslutList": [
                                    {
                                        "label": "当前单位",
                                        "value": "1"
                                    },
                                    {
                                        "label": "当前角色名称",
                                        "value": "2"
                                    }
                                ],
                                "selectList": [],
                                "actualPayment": 0,
                                "fieldTypeNames": "String",
                                "checked": false,
                                "componentName": "SingleLineDisableCard",
                                "baseTitle": "文本回显框",
                            }
                        ],
                        "checked": false,
                        "gridVal": 12,
                        "componentName": "GridItemCard",
                        "uuid": "a635e0d0-917b-437c-be84-9b326bb754aa"
                    },
                    {
                        "componentsList": [
                            {
                                "isRequired": true,
                                "componentType": 8,
                                "fieldNames": "use_car_people",
                                "icon": "ivu-icon ivu-icon-ios-create-outline",

                                "uuid": "9d2179aa-5a2b-473b-b942-a654c7692676",
                                "basePresetTxt": "请输入",
                                "resultVal": "",
                                "fieldTypes": "1",
                                "fieldTypeNames": "String",
                                "checked": false,
                                "componentName": "SingleLineCard",
                                "baseTitle": "单行文本",
                                "presetTxt": "请输入"
                            }
                        ],
                        "checked": false,
                        "gridVal": 12,
                        "componentName": "GridItemCard",
                        "uuid": "91c9c3b3-f1f4-4dc1-b58a-687dbd111920"
                    }
                ],
                "icon": "ivu-icon ivu-icon-md-person",
                "checked": false,
                "componentName": "GridCard",
                "baseTitle": "栅格组件",
                "uuid": "b1e576a1-9df7-4150-a1bc-20510ca1b0a5",
                "basePresetTxt": "拖拽控件到布局容器中",
                "resultVal": 2
            },
            {
                "componentType": 15,
                "componentsList": [
                    {
                        "componentsList": [
                            {
                                "isRequired": false,
                                "componentType": 8,
                                "fieldNames": "fellow",
                                "icon": "ivu-icon ivu-icon-ios-create-outline",

                                "uuid": "62d09847-a05f-4031-9f5c-e27f1867e205",
                                "basePresetTxt": "请输入",
                                "resultVal": "",
                                "fieldTypes": "1",
                                "fieldTypeNames": "String",
                                "checked": false,
                                "componentName": "SingleLineCard",
                                "baseTitle": "单行文本",
                                "presetTxt": "请输入"
                            }
                        ],
                        "checked": false,
                        "gridVal": 12,
                        "componentName": "GridItemCard",
                        "uuid": "08c8ff4a-b019-403e-8740-367d48edb982"
                    },
                    {
                        "componentsList": [
                            {
                                "isRequired": true,
                                "componentType": 8,
                                "fieldNames": "how_day",
                                "icon": "ivu-icon ivu-icon-ios-create-outline",

                                "uuid": "ae58a05b-a84d-4bbb-a17e-dcd208558cc1",
                                "basePresetTxt": "请输入",
                                "resultVal": "",
                                "fieldTypes": "1",
                                "fieldTypeNames": "String",
                                "checked": false,
                                "componentName": "SingleLineCard",
                                "baseTitle": "单行文本",
                                "presetTxt": "请输入"
                            }
                        ],
                        "checked": false,
                        "gridVal": 12,
                        "componentName": "GridItemCard",
                        "uuid": "6a0e8f2c-c16a-42fd-8cf9-c43ebde113ba"
                    }
                ],
                "icon": "ivu-icon ivu-icon-md-person",
                "checked": false,
                "componentName": "GridCard",
                "baseTitle": "栅格组件",
                "uuid": "fcceb368-4f31-498e-9ab4-21fd7ff97111",
                "basePresetTxt": "拖拽控件到布局容器中",
                "resultVal": 1
            },
            {
                "componentType": 15,
                "componentsList": [
                    {
                        "componentsList": [
                            {
                                "isRequired": true,
                                "componentType": 8,
                                "fieldNames": "place",
                                "icon": "ivu-icon ivu-icon-ios-create-outline",

                                "uuid": "dc234656-58f8-4c4f-9fcf-81fef0fd7fe8",
                                "basePresetTxt": "请输入",
                                "resultVal": "",
                                "fieldTypes": "1",
                                "fieldTypeNames": "String",
                                "checked": false,
                                "componentName": "SingleLineCard",
                                "baseTitle": "单行文本",
                                "presetTxt": "请输入"
                            }
                        ],
                        "checked": false,
                        "gridVal": 12,
                        "componentName": "GridItemCard",
                        "uuid": "f99f2b5b-0d3f-44d7-9219-dee2e149ebeb"
                    },
                    {
                        "componentsList": [
                            {
                                "isRequired": true,
                                "componentType": 11,
                                "fieldNames": "go_date_time",
                                "icon": "ivu-icon ivu-icon-ios-calendar-outline",

                                "uuid": "4839bdaf-5004-448f-a58b-f517e3c20ac5",
                                "basePresetTxt": "说明文字",
                                "resultVal": "",
                                "fieldTypeNames": "String",
                                "checked": false,
                                "componentName": "DateSelectionCard",
                                "baseTitle": "日期选择",
                                "presetTxt": "说明文字"
                            }
                        ],
                        "checked": false,
                        "gridVal": 12,
                        "componentName": "GridItemCard",
                        "uuid": "dae53172-d4c0-4ff5-ac8d-9c045bd96292"
                    }
                ],
                "icon": "ivu-icon ivu-icon-md-person",
                "checked": false,
                "componentName": "GridCard",
                "baseTitle": "栅格组件",
                "uuid": "15bf6091-edef-41ec-977b-625a212e9a9a",
                "basePresetTxt": "拖拽控件到布局容器中",
                "resultVal": 2
            },
            {
                "isRequired": true,
                "componentType": 9,
                "fieldNames": "use_car_reason",
                "icon": "ivu-icon ivu-icon-ios-create-outline",
                "uuid": "cbdbe955-417a-40d5-906d-b144d5f9b9f0",
                "basePresetTxt": "说明文字",
                "resultVal": "",
                "fieldTypeNames": "String",
                "checked": false,
                "componentName": "MultilineTextCard",
                "baseTitle": "多行文本",
                "presetTxt": "说明文字"
            }
        ]
        let obj = {
            "how_day": {
                "isDisable": true,
                "fieldTitle": "预计天数"
            },
            "fellow": {
                "isDisable": true,
                "fieldTitle": "同行人"
            },
            "bumen": {
                "isDisable": true,
                "fieldTitle": "用车部门"
            },
            "use_car_people": {
                "isDisable": true,
                "fieldTitle": "用车人"
            },
            "go_date_time": {
                "isDisable": true,
                "fieldTitle": "出车时间"
            },
            "use_car_reason": {
                "isDisable": true,
                "fieldTitle": "用车事由"
            },
            "place": {
                "isDisable": true,
                "fieldTitle": "办事地点"
            },
            "apply_user_name": {
                "isDisable": false,
                "fieldTitle": "申请人"
            }
        }

        const arrFn = (data) => {
            data.forEach((o, i) => {
                console.log(o.fieldNames)
                if (obj[o.fieldNames] && obj[o.fieldNames].isDisable) {
                    data.splice(i, 1)
                    new Set(arr)
                }
                if (o.componentsList) {
                    arrFn(o.componentsList)
                }
            });
        }
        console.time()
        arrFn(arr)
        console.log('--------------------------')
        arrFn(arr)
        console.timeEnd()

主要是删除了arr 里面的对象中 fieldNames 的值在 obj 对象中 isDisable 的属性为 true 的对象。

image.png

以上最终输出的结果为 apply_user_name 的对象,时间上只用了 1.3ms,性能还是可以的。

24人点赞 返回栏目 提问 分享一波

小礼物走一波,支持作者

还没有人赞赏,支持一波吧

留言(问题紧急可添加微信 xxl18963067593) 评论仅代表网友个人 留言列表

暂无留言,快来抢沙发吧!

本刊热文
网友在读
手机扫码查看 手机扫码查看