javascript - 从嵌套对象中删除特定对象
问题描述
我正在尝试从嵌套对象中删除特定对象:
这是我正在使用的原始数据和函数:
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”的递归函数。我不得不说该功能工作正常。
但我也想保留原始对象,但我不知道该怎么做。我希望你能帮助我
解决方案
你的答案
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)
推荐阅读
- javascript - JS 对象默认单引号行为
- sql - 如何将一行中的所有值连接成一个字符串?
- ios - iOS 项目中的 import <> vs import "" 有什么区别
- node.js - 尝试安装 npm 后出现错误
- javascript - 去抖动和返回
- ecmascript-6 - 如何在 Eclipse swt 应用程序中使用 Windows Edge 浏览器而不是 IE?
- php - 如何在 laravel 中撤销多重身份验证中的令牌?
- spring-boot - keycloak授权服务器如何使用spring rest适配器验证授权规则
- android - 如何返回异步等待承诺
- android - sdk 的 targetSdkVersion 更改为 26