I’ve been trying to filter an array of objects using recursion and javascript. I was able to recursively transform the initial JSON I get from an API (more than 5k lines) down to the data I need but I seem to be unable to filter out the undefined entries. Below is a sample of the object I am trying clean.
The idea is to have only the entries whose innermost child member does not only contain a parent, you can see this in the element whose innermost child has the name "loan". The level these childs can appear are not fixed as well as how many of them at a time.
{
"root": [
{
"parent": {
"parentId": null,
"parentName": null
},
"child": [
{
"parent": {
"parentId": "1000641",
"parentName": "parent120"
}
},
{
"parent": {
"parentId": "1000641",
"parentName": "parent121"
}
}
]
},
{
"parent": {
"parentId": null,
"parentName": null
}
},
{
"parent": {
"parentId": null,
"parentName": null
},
"child": [
{
"parent": {
"parentId": "1000607",
"parentName": "parent122"
}
},
{
"parent": {
"parentId": "1000607",
"parentName": "parent123"
}
},
{
"parent": {
"parentId": "1000607",
"parentName": "parent124"
}
}
]
},
{
"parent": {
"parentId": null,
"parentName": null
},
"child": [
{
"parent": {
"parentId": "1000604",
"parentName": "parent125"
}
},
{
"parent": {
"parentId": "1000604",
"parentName": "parent126"
}
}
]
},
{
"parent": {
"parentId": null,
"parentName": null
},
"child": [
{
"parent": {
"parentId": "1000657",
"parentName": "parent127"
},
"child": [
{
"parent": {
"parentId": "1000529",
"parentName": "parent128"
}
},
{
"parent": {
"parentId": "1000529",
"parentName": "parent129"
}
},
{
"parent": {
"parentId": "1000529",
"parentName": "parent111"
}
},
{
"parent": {
"parentId": "1000529",
"parentName": "parent100"
}
},
{
"parent": {
"parentId": "1000529",
"parentName": "parent90"
}
}
]
},
{
"parent": {
"parentId": "1000657",
"parentName": "parent91"
},
"child": [
{
"parent": {
"parentId": "1000535",
"parentName": "parent92"
}
},
{
"parent": {
"parentId": "1000535",
"parentName": "parent92"
}
},
{
"parent": {
"parentId": "1000535",
"parentName": "parent92"
}
},
{
"parent": {
"parentId": "1000535",
"parentName": "parent92"
}
}
]
},
{
"parent": {
"parentId": "1000657",
"parentName": "parent234"
},
"child": [
{
"parent": {
"parentId": "1000524",
"parentName": "parent230"
}
},
{
"parent": {
"parentId": "1000524",
"parentName": "parent231"
}
},
{
"parent": {
"parentId": "1000524",
"parentName": "parent232"
}
},
{
"parent": {
"parentId": "1000524",
"parentName": "parent233"
}
}
]
},
{
"parent": {
"parentId": "1000657",
"parentName": "parent234"
}
}
]
},
{
"parent": {
"parentId": null,
"parentName": null
}
},
{
"parent": {
"parentId": null,
"parentName": null
}
},
{
"parent": {
"parentId": null,
"parentName": null
}
},
{
"parent": {
"parentId": null,
"parentName": null
}
},
{
"parent": {
"parentId": null,
"parentName": null
},
"child": [
{
"parent": {
"parentId": "1000611",
"parentName": "loan parent"
},
"child": [
{
"parent": {
"parentId": "1000612",
"parentName": "loan"
},
"child": {
"name": "loan1",
"id": "1000613",
"treeDepth": 3
}
},
{
"parent": {
"parentId": "1000612",
"parentName": "loan"
},
"child": {
"name": "loan2",
"id": "1000615",
"treeDepth": 3
}
},
{
"parent": {
"parentId": "1000612",
"parentName": "loan"
},
"child": {
"name": "loan3",
"id": "1000614",
"treeDepth": 3
}
}
]
},
{
"parent": {
"parentId": "1000611",
"parentName": "DIGI VAS CAY"
},
"child": [
{
"parent": {
"parentId": "1000616",
"parentName": "bundle1"
}
},
{
"parent": {
"parentId": "1000616",
"parentName": "bundle2"
}
}
]
}
]
},
{
"parent": {
"parentId": null,
"parentName": null
},
"child": [
{
"parent": {
"parentId": "1000645",
"parentName": "parent6"
},
"child": [
{
"parent": {
"parentId": "1000646",
"parentName": "parent8"
}
},
{
"parent": {
"parentId": "1000646",
"parentName": "parent8"
}
},
{
"parent": {
"parentId": "1000646",
"parentName": "parent8"
}
},
{
"parent": {
"parentId": "1000646",
"parentName": "parent8"
}
}
]
},
{
"parent": {
"parentId": "1000645",
"parentName": "parent5"
},
"child": [
{
"parent": {
"parentId": "1000651",
"parentName": "parent6"
}
},
{
"parent": {
"parentId": "1000651",
"parentName": "parent6"
}
}
]
},
{
"parent": {
"parentId": "1000645",
"parentName": "parent5"
},
"child": [
{
"parent": {
"parentId": "1000654",
"parentName": "parent123"
}
},
{
"parent": {
"parentId": "1000654",
"parentName": "parent123"
}
}
]
}
]
}
]
}
I have tried filtering with the following code with no avail
function deleteNoChildEntries(membersArray) {
return membersArray.filter(function magic(element) {
if(Array.isArray(element.child)) {
element.child.filter(magic);
}
return element.child !== undefined;
});
}
I expect to get an array with only one element containing the tree spaning the childs that exist and are not populated only by parents. Here’s what I expect to get
[
{
"parent": {
"parentId": null,
"parentName": null
},
"child": [
{
"parent": {
"parentId": "1000611",
"parentName": "loan parent"
},
"child": [
{
"parent": {
"parentId": "1000612",
"parentName": "loan"
},
"child": {
"name": "loan1",
"id": "1000613",
"treeDepth": 3
}
},
{
"parent": {
"parentId": "1000612",
"parentName": "loan"
},
"child": {
"name": "loan2",
"id": "1000615",
"treeDepth": 3
}
},
{
"parent": {
"parentId": "1000612",
"parentName": "loan"
},
"child": {
"name": "loan3",
"id": "1000614",
"treeDepth": 3
}
}
]
}
]
}
]
and here is what my code is doing
[
{
"parent": {
"parentId": null,
"parentName": null
},
"child": [
{
"parent": {
"parentId": "1000641",
"parentName": "parent120"
}
},
{
"parent": {
"parentId": "1000641",
"parentName": "parent121"
}
}
]
},
{
"parent": {
"parentId": null,
"parentName": null
},
"child": [
{
"parent": {
"parentId": "1000607",
"parentName": "parent122"
}
},
{
"parent": {
"parentId": "1000607",
"parentName": "parent123"
}
},
{
"parent": {
"parentId": "1000607",
"parentName": "parent124"
}
}
]
},
{
"parent": {
"parentId": null,
"parentName": null
},
"child": [
{
"parent": {
"parentId": "1000604",
"parentName": "parent125"
}
},
{
"parent": {
"parentId": "1000604",
"parentName": "parent126"
}
}
]
},
{
"parent": {
"parentId": null,
"parentName": null
},
"child": [
{
"parent": {
"parentId": "1000657",
"parentName": "parent127"
},
"child": [
{
"parent": {
"parentId": "1000529",
"parentName": "parent128"
}
},
{
"parent": {
"parentId": "1000529",
"parentName": "parent129"
}
},
{
"parent": {
"parentId": "1000529",
"parentName": "parent111"
}
},
{
"parent": {
"parentId": "1000529",
"parentName": "parent100"
}
},
{
"parent": {
"parentId": "1000529",
"parentName": "parent90"
}
}
]
},
{
"parent": {
"parentId": "1000657",
"parentName": "parent91"
},
"child": [
{
"parent": {
"parentId": "1000535",
"parentName": "parent92"
}
},
{
"parent": {
"parentId": "1000535",
"parentName": "parent92"
}
},
{
"parent": {
"parentId": "1000535",
"parentName": "parent92"
}
},
{
"parent": {
"parentId": "1000535",
"parentName": "parent92"
}
}
]
},
{
"parent": {
"parentId": "1000657",
"parentName": "parent234"
},
"child": [
{
"parent": {
"parentId": "1000524",
"parentName": "parent230"
}
},
{
"parent": {
"parentId": "1000524",
"parentName": "parent231"
}
},
{
"parent": {
"parentId": "1000524",
"parentName": "parent232"
}
},
{
"parent": {
"parentId": "1000524",
"parentName": "parent233"
}
}
]
},
{
"parent": {
"parentId": "1000657",
"parentName": "parent234"
}
}
]
},
{
"parent": {
"parentId": null,
"parentName": null
},
"child": [
{
"parent": {
"parentId": "1000611",
"parentName": "loan parent"
},
"child": [
{
"parent": {
"parentId": "1000612",
"parentName": "loan"
},
"child": {
"name": "loan1",
"id": "1000613",
"treeDepth": 3
}
},
{
"parent": {
"parentId": "1000612",
"parentName": "loan"
},
"child": {
"name": "loan2",
"id": "1000615",
"treeDepth": 3
}
},
{
"parent": {
"parentId": "1000612",
"parentName": "loan"
},
"child": {
"name": "loan3",
"id": "1000614",
"treeDepth": 3
}
}
]
},
{
"parent": {
"parentId": "1000611",
"parentName": "DIGI VAS CAY"
},
"child": [
{
"parent": {
"parentId": "1000616",
"parentName": "bundle1"
}
},
{
"parent": {
"parentId": "1000616",
"parentName": "bundle2"
}
}
]
}
]
},
{
"parent": {
"parentId": null,
"parentName": null
},
"child": [
{
"parent": {
"parentId": "1000645",
"parentName": "parent6"
},
"child": [
{
"parent": {
"parentId": "1000646",
"parentName": "parent8"
}
},
{
"parent": {
"parentId": "1000646",
"parentName": "parent8"
}
},
{
"parent": {
"parentId": "1000646",
"parentName": "parent8"
}
},
{
"parent": {
"parentId": "1000646",
"parentName": "parent8"
}
}
]
},
{
"parent": {
"parentId": "1000645",
"parentName": "parent5"
},
"child": [
{
"parent": {
"parentId": "1000651",
"parentName": "parent6"
}
},
{
"parent": {
"parentId": "1000651",
"parentName": "parent6"
}
}
]
},
{
"parent": {
"parentId": "1000645",
"parentName": "parent5"
},
"child": [
{
"parent": {
"parentId": "1000654",
"parentName": "parent123"
}
},
{
"parent": {
"parentId": "1000654",
"parentName": "parent123"
}
}
]
}
]
}
]
I have set up a code pen for testing https://codepen.io/mathSychs/pen/eYbmwLp
2
Answers
Question Tip
You need to make your data the smallest possible to demonstrate your problem the array is too big and I think there is an error in the tree somewhere.
I think this is what you want:
This might also work for you:
The simple way is to convert JSON into JS object. This snip of code may help you.
as you see, checkRoot() is the recursive function on every object of main root array. so, you must call it using loop.
this code can be rewritten with more efficiency way. but it will solve your problem.