Say I have a json structure that looks like this (directory tree structure) that I want to filter using javascript. I have asked a similar question before, but the requirements have changed and now I need the keep the structure after filtering.
const data = {
"item1": {
"item1.1": {
"item1.1.1": {
"item1.1.1.1": {
"attr1": [],
"attr2": "",
"attr3": [],
"status" : "ERROR"
}
}
},
"item1.2": {
"item1.2.1": {
"item1.2.1.1": {
"attr1": [],
"attr2": "",
"attr3": [],
"status" : "WARNING"
}
}
}
},
"item2": {
"item2.1": {
"item2.1.1": {
"item2.1.1.1": {
"attr1": [],
"attr2": "",
"attr3": [],
"status" : "WARNING"
}
},
"item2.1.2": {
"item2.1.2.1": {
"attr1": [],
"attr2": "",
"attr3": [],
"status" : "OK"
},
"item2.1.2.2": {
"attr1": [],
"attr2": "",
"attr3": [],
"status" : "WARNING"
}
}
}
},
"item3": {
"item3.1": {
"item3.1.1": {
"item3.1.1.1": {
"attr1": [],
"attr2": "",
"attr3": [],
"status" : "OK"
}
},
"item3.1.2": {
"attr1": [],
"attr2": "",
"attr3": [],
"status" : "ERROR"
}
}
}
}
Essentially, I want to create a function that lets me remove all the node items (and empty branches) that don't contain a certain status (i.e Warning) and keep the rest within the original structure. This would have to be done recursively because the depth of the nested structure is unknown. For example, filterByStatus(data, "WARNING") would return something like:
{
"item1": {
"item1.2": {
"item1.2.1": {
"item1.2.1.1": {
"attr1": [],
"attr2": "",
"attr3": [],
"status" : "WARNING"
}
}
}
},
"item2": {
"item2.1": {
"item2.1.1": {
"item2.1.1.1": {
"attr1": [],
"attr2": "",
"attr3": [],
"status" : "WARNING"
}
},
"item2.1.2": {
"item2.1.2.2": {
"attr1": [],
"attr2": "",
"attr3": [],
"status" : "WARNING"
}
}
}
}
}
