skip to Main Content

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


  1. 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:

    const mockupData = [
      {
        parent: {
          parentId: null,
          parentName: null,
        },
        child: [
          {
            parent: {
              parentId: "1000641",
              parentName: "parent120",
            },
          },
          {
            parent: {
              parentId: "1000641",
              parentName: "parent121",
            },
          },
        ],
      },
      // ... (rest of your data)
    ];
    
    function removeDuplicates(jsonData: any[]) {
      const seen = new Set();
      return jsonData.filter((item) => {
        if (item.child) {
          item.child = removeDuplicates(item.child);
          return item.child.length > 0;
        } else if (
          item.parent &&
          item.parent.parentId !== null &&
          item.parent.parentName !== null
        ) {
          // Create a key for the innermost child excluding the 'name' property
          const key = JSON.stringify(item.parent, (k, v) =>
            k === "parentName" ? undefined : v,
          );
    
          // Check if we've seen this key before
          if (!seen.has(key)) {
            seen.add(key);
            return true;
          }
          return false;
        } else {
          return true;
        }
      });
    }
    
    const filteredResult = removeDuplicates(mockupData);
    console.log(JSON.stringify(filteredResult, null, 4));
    

    This might also work for you:

    function removeEntriesWithUndefinedLastLevel(arr) {
      for (let i = arr.length - 1; i >= 0; i--) {
        if (typeof arr[i] === 'object' && !Array.isArray(arr[i])) {
          // If the current element is an object (not an array)
          // Check if its properties are also objects
          const subObject = arr[i];
          const subObjectKeys = Object.keys(subObject);
    
          if (subObjectKeys.length === 1 && subObject[subObjectKeys[0]] === undefined) {
            // If there is only one property, and it is undefined,
            // remove this entry from the array
            arr.splice(i, 1);
          } else {
            // Recursively check the nested objects
            removeEntriesWithUndefinedLastLevel(subObjectKeys.map(key => subObject[key]));
          }
        }
      }
    }
    
    Login or Signup to reply.
  2. The simple way is to convert JSON into JS object. This snip of code may help you.

        const jsonObj = JSON.parse(jsonText);
    
    const isObject = (value) => {
        return !!(value && typeof value === "object" && !Array.isArray(value));
    };
    
    
    var checkRoot = function (root) {
        if (isObject(root) && !root.child) {
            return false;
        }
        if (isObject(root) && isObject(root.child)) {
            return true;
        }
        for (var i = 0; i < root.child.length; i++) {
    
            var toKeep = checkRoot(root.child[i]);
            if (!toKeep) {
                root.child.splice(i, 1);
                i--;
            }
        }
        return !!root.child.length;
    }
    
    var startChecking = function (jsonObj) {
        for (var i = 0; i < jsonObj.root.length; i++) {
            var toKeep = checkRoot(jsonObj.root[i]);
            if (!toKeep) {
                jsonObj.root.splice(i, 1);
                i--;
            }
        }
        return jsonObj;
    }
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search