删除多维数组中包含键值的数组

Delete array which contains key value in multi-dimensional array

如何删除多维数组中包含匹配键值的数组?

我只使用 javascript,没有框架。

我正在用以下内容解析以下数组:

"services": [
    {
        "_id": "62473476313228338b3320cc",
        "name": "test",
        "fields": [
            {
                "name": "this",
                "type": "mulDropdown",
                "_id": "624f19bbec96c92076e98095",
                "data": [
                    {
                        "_id": "6250aaa4e5d0f222d74311d3",
                        "name": "this too",
                        "hidden": "0"
                    },
                    {
                        "_id": "62546392fa98b17106d35d7c",
                        "name": "this three",
                        "hidden": "0"
                    },
                    {
                        "_id": "6254639efa98b17106d35d7d",
                        "name": "this4",
                        "hidden": "1"
                    }
                ],
                "hidden": "0"
            },
            {
                "isRequire": false,
                "name": "another this",
                "type": "dropdown",
                "_id": "624f3bb4fda95e5fdf978016",
                "data": [
                    {
                        "_id": "625463d5fa98b17106d35d80",
                        "name": "another",
                        "hidden": "1"
                    },
                    {
                        "_id": "625463e6fa98b17106d35d81",
                        "name": "another2",
                    }
                ],
                "hidden": "0"
            }
          ]
      }
    
]


services.forEach((item, index) => {

item.fields.forEach((i, ind) => {

if (i.type == "mulDropdown") {

i.data.forEach(function (item, index) {

if (item.hidden == '1') {
delete item["name"];

此时我可以看到值并可以使用 'delete item' 删除键,但我需要使用 'delete data[2]'

之类的内容删除整个数据数组

我已经尝试了各种过滤器、拼接、删除示例,但我无法设法让它在我的阵列上工作。

所需数组:

"services": [
    {
        "_id": "62473476313228338b3320cc",
        "name": "test",
        "fields": [
            {
                "name": "this",
                "type": "mulDropdown",
                "_id": "624f19bbec96c92076e98095",
                "data": [
                    {
                        "_id": "6250aaa4e5d0f222d74311d3",
                        "name": "this too",
                        "hidden": "0"
                    },
                    {
                        "_id": "62546392fa98b17106d35d7c",
                        "name": "this three",
                        "hidden": "0"
                    }
                ],
                "hidden": "0"
            },
            {
                "isRequire": false,
                "name": "another this",
                "type": "dropdown",
                "_id": "624f3bb4fda95e5fdf978016",
                "data": [
                    {
                        "_id": "625463d5fa98b17106d35d80",
                        "name": "another",
                        "hidden": "1"
                    },
                    {
                        "_id": "625463e6fa98b17106d35d81",
                        "name": "another2",
                    }
                ],
                "hidden": "0"
            }
          ]
      }
    
]

您可以通过键映射并根据 "type" = "mulDropdown""hidden" !== "1" 过滤数据 属性。请注意,这将修改原始对象。但是从你的例子来看,你似乎可以接受。

let obj = {
  services: [
    {
      _id: '62473476313228338b3320cc',
      name: 'test',
      fields: [
        {
          name: 'this',
          type: 'mulDropdown',
          _id: '624f19bbec96c92076e98095',
          data: [
            {
              _id: '6250aaa4e5d0f222d74311d3',
              name: 'this too',
              hidden: '0'
            },
            {
              _id: '62546392fa98b17106d35d7c',
              name: 'this three',
              hidden: '0'
            },
            {
              _id: '6254639efa98b17106d35d7d',
              name: 'this4',
              hidden: '1'
            }
          ],
          hidden: '0'
        },
        {
          isRequire: false,
          name: 'another this',
          type: 'dropdown',
          _id: '624f3bb4fda95e5fdf978016',
          data: [
            {
              _id: '625463d5fa98b17106d35d80',
              name: 'another',
              hidden: '1'
            },
            {
              _id: '625463e6fa98b17106d35d81',
              name: 'another2'
            }
          ],
          hidden: '0'
        }
      ]
    }
  ]
}

obj.services.map((s) => {
  s.fields.map((f) => {
    f.data = f.type === 'mulDropdown' ? f.data.filter((d) => d.hidden !== '1') : f.data
  })
})

console.log(obj)