首页 > 解决方案 > 从嵌套对象中删除特定对象

问题描述

我正在尝试从嵌套对象中删除特定对象:

这是我正在使用的原始数据和函数:

const allList = [
    {
        "id": "listGroup",
        "condition": "AND",
        "data": [
            {
                "id": "611901",
                "name": "1",
                "value": "1",
                "data": "6137",
                "path": "Automotive Miscellaneous>Car Rental - Personal Use>Times/Last 12 Months",
                "uniqueId": "pmds-dragdrop__61190158"
            },
            {
                "id": "listGroup_1",
                "children": {
                    "key": null,
                    "ref": null,
                    "props": {
                        "children": {
                            "type": "div",
                            "key": null,
                            "ref": null,
                            "props": {
                                "style": {
                                    "display": "flex",
                                    "boxSizing": "border-box"
                                },
                                "children": [
                                    {
                                        "type": {
                                            "propTypes": {},
                                            "defaultProps": {
                                                "id": null,
                                                "children": null,
                                                "className": null,
                                                "disabled": false,
                                                "required": false,
                                                "options": [],
                                                "multiple": false,
                                                "placeholder": null,
                                                "error": null,
                                                "onChange": null,
                                                "display": "pillBelow",
                                                "invert": false
                                            },
                                            "ComponentName": "Dropdown",
                                            "package": "@publicismedia-ds/ui-dropdown",
                                            "version": "2.1.0"
                                        },
                                        "key": null,
                                        "ref": null,
                                        "props": {
                                            "options": [
                                                {
                                                    "label": "AND",
                                                    "value": "AND"
                                                },
                                                {
                                                    "label": "OR",
                                                    "value": "OR"
                                                },
                                                {
                                                    "label": "AND NOT",
                                                    "value": "AND NOT"
                                                },
                                                {
                                                    "label": "OR NOT",
                                                    "value": "OR NOT"
                                                }
                                            ],
                                            "value": {
                                                "label": "AND",
                                                "value": "AND"
                                            },
                                            "children": " ",
                                            "id": null,
                                            "className": null,
                                            "disabled": false,
                                            "required": false,
                                            "multiple": false,
                                            "placeholder": null,
                                            "error": null,
                                            "display": "pillBelow",
                                            "invert": false
                                        },
                                        "_owner": null,
                                        "_store": {}
                                    },
                                    {
                                        "type": {
                                            "propTypes": {},
                                            "defaultProps": {
                                                "className": "",
                                                "list": null,
                                                "onChange": null,
                                                "id": "",
                                                "selectAll": false,
                                                "placeholder": "Drop keys here",
                                                "children": null,
                                                "allowDuplicates": true,
                                                "allowed": null,
                                                "notAllowed": null,
                                                "border": false,
                                                "removeDraggedItems": true,
                                                "reorder": true,
                                                "deleteItem": false,
                                                "errorMessage": "Not allowed",
                                                "onDragStart": null,
                                                "onDrop": null,
                                                "__TYPE": "DragDrop"
                                            },
                                            "componentName": "DragDrop",
                                            "package": "@publicismedia-ds/ui-drag-drop",
                                            "version": "2.2.0-beta.1"
                                        },
                                        "key": null,
                                        "ref": null,
                                        "props": {
                                            "list": {
                                                "id": "listGroup_1",
                                                "condition": "OR",
                                                "data": [
                                                    {
                                                        "id": "611908",
                                                        "name": "4",
                                                        "value": "4",
                                                        "data": "432",
                                                        "path": "Automotive Miscellaneous>Car Rental - Personal Use>Times/Last 12 Months",
                                                        "uniqueId": "pmds-dragdrop__61190834"
                                                    }
                                                ]
                                            },
                                            "id": "listGroup_1",
                                            "selectAll": false,
                                            "placeholder": "Drop keys here",
                                            "deleteItem": true,
                                            "border": true,
                                            "allowDuplicates": false,
                                            "errorMessage": "Not allowed!",
                                            "className": "",
                                            "children": null,
                                            "allowed": null,
                                            "notAllowed": null,
                                            "removeDraggedItems": true,
                                            "reorder": true,
                                            "onDragStart": null,
                                            "onDrop": null,
                                            "__TYPE": "DragDrop"
                                        },
                                        "_owner": null,
                                        "_store": {}
                                    }
                                ]
                            },
                            "_owner": null,
                            "_store": {}
                        }
                    },
                    "_owner": null,
                    "_store": {}
                },
                "uniqueId": "pmds-dragdrop__listGroup_159"
            }
        ]
    },
    {
        "id": "listGroup_1",
        "condition": "OR",
        "data": [
            {
                "id": "611908",
                "name": "4",
                "value": "4",
                "data": "432",
                "path": "Automotive Miscellaneous>Car Rental - Personal Use>Times/Last 12 Months",
                "uniqueId": "pmds-dragdrop__61190834"
            }
        ]
    }
]

console.log(allList);

function filterObject(obj, key) {
  for (var i in obj) {
    if (!obj.hasOwnProperty(i)) continue;
    if (i == key) {
      delete obj[key];
    } else if (typeof obj[i] == "object") {
      filterObject(obj[i], key);
    }
  }
  return obj;
}

console.log(filterObject(allList, "children"));

我想删除所有带有“children”键的对象,并且我正在使用一个名为“filterObject”的递归函数。我不得不说该功能工作正常。

但我也想保留原始对象,但我不知道该怎么做。我希望你能帮助我

标签: javascriptarraysrecursion

解决方案


你的答案

const allList = [
    {
        "id": "listGroup",
        "condition": "AND",
        "data": [
            {
                "id": "611901",
                "name": "1",
                "value": "1",
                "data": "6137",
                "path": "Automotive Miscellaneous>Car Rental - Personal Use>Times/Last 12 Months",
                "uniqueId": "pmds-dragdrop__61190158"
            },
            {
                "id": "listGroup_1",
                "children": {
                    "key": null,
                    "ref": null,
                    "props": {
                        "children": {
                            "type": "div",
                            "key": null,
                            "ref": null,
                            "props": {
                                "style": {
                                    "display": "flex",
                                    "boxSizing": "border-box"
                                },
                                "children": [
                                    {
                                        "type": {
                                            "propTypes": {},
                                            "defaultProps": {
                                                "id": null,
                                                "children": null,
                                                "className": null,
                                                "disabled": false,
                                                "required": false,
                                                "options": [],
                                                "multiple": false,
                                                "placeholder": null,
                                                "error": null,
                                                "onChange": null,
                                                "display": "pillBelow",
                                                "invert": false
                                            },
                                            "ComponentName": "Dropdown",
                                            "package": "@publicismedia-ds/ui-dropdown",
                                            "version": "2.1.0"
                                        },
                                        "key": null,
                                        "ref": null,
                                        "props": {
                                            "options": [
                                                {
                                                    "label": "AND",
                                                    "value": "AND"
                                                },
                                                {
                                                    "label": "OR",
                                                    "value": "OR"
                                                },
                                                {
                                                    "label": "AND NOT",
                                                    "value": "AND NOT"
                                                },
                                                {
                                                    "label": "OR NOT",
                                                    "value": "OR NOT"
                                                }
                                            ],
                                            "value": {
                                                "label": "AND",
                                                "value": "AND"
                                            },
                                            "children": " ",
                                            "id": null,
                                            "className": null,
                                            "disabled": false,
                                            "required": false,
                                            "multiple": false,
                                            "placeholder": null,
                                            "error": null,
                                            "display": "pillBelow",
                                            "invert": false
                                        },
                                        "_owner": null,
                                        "_store": {}
                                    },
                                    {
                                        "type": {
                                            "propTypes": {},
                                            "defaultProps": {
                                                "className": "",
                                                "list": null,
                                                "onChange": null,
                                                "id": "",
                                                "selectAll": false,
                                                "placeholder": "Drop keys here",
                                                "children": null,
                                                "allowDuplicates": true,
                                                "allowed": null,
                                                "notAllowed": null,
                                                "border": false,
                                                "removeDraggedItems": true,
                                                "reorder": true,
                                                "deleteItem": false,
                                                "errorMessage": "Not allowed",
                                                "onDragStart": null,
                                                "onDrop": null,
                                                "__TYPE": "DragDrop"
                                            },
                                            "componentName": "DragDrop",
                                            "package": "@publicismedia-ds/ui-drag-drop",
                                            "version": "2.2.0-beta.1"
                                        },
                                        "key": null,
                                        "ref": null,
                                        "props": {
                                            "list": {
                                                "id": "listGroup_1",
                                                "condition": "OR",
                                                "data": [
                                                    {
                                                        "id": "611908",
                                                        "name": "4",
                                                        "value": "4",
                                                        "data": "432",
                                                        "path": "Automotive Miscellaneous>Car Rental - Personal Use>Times/Last 12 Months",
                                                        "uniqueId": "pmds-dragdrop__61190834"
                                                    }
                                                ]
                                            },
                                            "id": "listGroup_1",
                                            "selectAll": false,
                                            "placeholder": "Drop keys here",
                                            "deleteItem": true,
                                            "border": true,
                                            "allowDuplicates": false,
                                            "errorMessage": "Not allowed!",
                                            "className": "",
                                            "children": null,
                                            "allowed": null,
                                            "notAllowed": null,
                                            "removeDraggedItems": true,
                                            "reorder": true,
                                            "onDragStart": null,
                                            "onDrop": null,
                                            "__TYPE": "DragDrop"
                                        },
                                        "_owner": null,
                                        "_store": {}
                                    }
                                ]
                            },
                            "_owner": null,
                            "_store": {}
                        }
                    },
                    "_owner": null,
                    "_store": {}
                },
                "uniqueId": "pmds-dragdrop__listGroup_159"
            }
        ]
    },
    {
        "id": "listGroup_1",
        "condition": "OR",
        "data": [
            {
                "id": "611908",
                "name": "4",
                "value": "4",
                "data": "432",
                "path": "Automotive Miscellaneous>Car Rental - Personal Use>Times/Last 12 Months",
                "uniqueId": "pmds-dragdrop__61190834"
            }
        ]
    }
]



function filterObject(obj, key) {
  for (var i in obj) {
    if (!obj.hasOwnProperty(i)) continue;
    if (i == key) {
      delete obj[key];
    } else if (typeof obj[i] == "object") {
      filterObject(obj[i], key);
    }
  }
  return obj;
}
const filtered = filterObject(JSON.parse(JSON.stringify(allList)), "children")
console.log(filtered)
console.log(allList);

你在做什么

const arr = [1,2,3];

const arr2 = arr.reverse();

console.log(arr);

console.log(arr2)

你需要什么

您必须复制数组而不是引用数组

const arr = [1,2,3];

const arr2 = [...arr].reverse();

console.log(arr);

console.log(arr2)

但在你的情况下,它不会起作用,为什么

因为扩展运算符创建一个浅拷贝意味着所有深层对象都连接在复制的数组和原始数组之间

方法二

const arr = [1,2,3];

const arr2 = JSON.parse(JSON.stringify(arr)).reverse();

console.log(arr);

console.log(arr2)


推荐阅读