this seems quite an easy task, but for some reason am stuck! I tried several solutions I found in SE, mainly to recursively iterate the array/object, such as How to iterate over an array and remove elements in JavaScript and Looping through array and removing items, without breaking for loop (and many others) but with no luck!
I have this array
{
"condition": "AND",
"rules": [
{
"id": "loc1",
"type": "string",
"input": "text",
"operator": "equal",
"value": "test0"
},
{
"id": "sessionIgnore",
"type": "string",
"input": "text",
"operator": "equal",
"value": "ignore0"
},
{
"condition": "AND",
"rules": [
{
"id": "loc1",
"type": "string",
"input": "text",
"operator": "equal",
"value": "test1"
},
{
"id": "sessionIgnore",
"type": "string",
"input": "text",
"operator": "equal",
"value": "ignore1"
},
{
"condition": "AND",
"rules": [
{
"id": "loc1",
"type": "string",
"input": "text",
"operator": "equal",
"value": "test2"
},
{
"id": "sessionIgnore",
"type": "string",
"input": "text",
"operator": "equal",
"value": "ignore2"
}
]
}
]
},
{
"condition": "AND",
"rules": [
{
"id": "loc1",
"type": "string",
"input": "text",
"operator": "equal",
"value": "test3"
},
{
"id": "sessionIgnore",
"type": "string",
"input": "text",
"operator": "equal",
"value": "ignore3"
}
]
}
],
"valid": true
}
and i want to remove all sessionIgnore elements
{
"condition": "AND",
"rules": [
{
"id": "loc1",
"type": "string",
"input": "text",
"operator": "equal",
"value": "test0"
},
{
"condition": "AND",
"rules": [
{
"id": "loc1",
"type": "string",
"input": "text",
"operator": "equal",
"value": "test1"
},
{
"condition": "AND",
"rules": [
{
"id": "loc1",
"type": "string",
"input": "text",
"operator": "equal",
"value": "test2"
}
]
}
]
},
{
"condition": "AND",
"rules": [
{
"id": "loc1",
"type": "string",
"input": "text",
"operator": "equal",
"value": "test3"
}
]
}
],
"valid": true
}
what seems to be the problem in my case: in both cases, I go through the array recursively
- the
delete x/array.slice()
approach: seems to keep the references to an empty array element, which I then have to call a cleanup method and I would prefer to avoid this - I created an arrayRemove() function that removes the x element from the given array, but in this case, I seem to have a logic issue, and I can't parse anything after the second level of the array
any help is appreciated!
Edit: in case a rules object has only a sessionIgnore element in it, I want the whole section removed i.e. referencing the above array, if the sessionIgnore with value ignore2 was the only object in its 'rules' group, I want the whole section removed edit2: added the expected result
edit in regards with nina's conversation:
in case the object is like this:
{
"condition": "AND",
"rules": [
{
"id": "loc1",
"type": "string",
"input": "text",
"operator": "equal",
"value": "test1"
},
{
"id": "sessionIgnore",
"type": "string",
"input": "text",
"operator": "equal",
"value": "ignore1"
},
{
"condition": "AND",
"rules": [
{
"id": "sessionIgnore",
"type": "string",
"input": "text",
"operator": "equal",
"value": "ignore2"
}
]
}
]
}
i want this
{
"condition": "AND",
"rules": [
{
"id": "loc1",
"type": "string",
"input": "text",
"operator": "equal",
"value": "test1"
}
]
}