0
[
  {
    "PRODUCT_CATEGORY_ID": "1",
    "NAME": "OTOMOBİL PARÇALARI",
    "DESCRIPTION": "Araba Parçaları",
    "ACTIVE": "True",
    "PARENT_PRODUCT_CATEGORY_ID": "",
    "children": [
      {
        "PRODUCT_CATEGORY_ID": "3",
        "NAME": "HONDA PARÇALARI",
        "DESCRIPTION": "Honda Parçaları",
        "ACTIVE": "True",
        "PARENT_PRODUCT_CATEGORY_ID": "1"
      },
      {
        "PRODUCT_CATEGORY_ID": "4",
        "NAME": "RENAULT PARÇALARI",
        "DESCRIPTION": "Renault Parçaları",
        "ACTIVE": "True",
        "PARENT_PRODUCT_CATEGORY_ID": "1",
        "children": [
          {
            "PRODUCT_CATEGORY_ID": "5",
            "NAME": "MINIMAL RENAULT PARÇALARI",
            "DESCRIPTION": "",
            "ACTIVE": "True",
            "PARENT_PRODUCT_CATEGORY_ID": "4"
          }
        ]
      }
    ]
  }
]

I have a json with nested children and I want the paths of this object's children. nested children can be even more.I want as output is a string array.

[
  "OTOMOBİL PARÇALARI"
  "OTOMOBİL PARÇALARI > HONDA PARÇALARI"
  "OTOMOBİL PARÇALARI > RENAULT PARÇALARI"
  "OTOMOBİL PARÇALARI > RENAULT PARÇALARI > MINIMAL RENAULT PARÇALARI"
]

I tried

function findAllChildren (id, results, depth) {
    for (d in data) {
        if (data[d].parent == id) {
            data[d].depth = depth
            results.push(data[d])
            findAllChildren(data[d].id, results, depth + 1)
        }
    }
}

var results = []
findAllChildren(1, results, 0)

return results.map(function (element) { return Array(element.depth + 1).join(' -> ') + element.name})

Thank you for your help in advance, I searched and couldn't find it, you can share it with me in the link.

5
  • 1
    "I have a json object..." - No, because there's no such thing as a "JSON Object". What you have there is an array with one object. Commented Sep 17, 2021 at 10:33
  • 2
    "What I want as output is a string array." - What have you tried so far to solve this on your own? Commented Sep 17, 2021 at 10:33
  • 2
    Recursively list all object property paths from JSON Commented Sep 17, 2021 at 10:37
  • Thank you for this link. But this json is not same my json so. It didn't solve the problem. Commented Sep 17, 2021 at 11:06
  • 1
    Then make the necessary adjustments so it fits your object (there's still no JSON in your question). That said... The linked question should work with every type of object. And if not then the linked dupe target in it will... Commented Sep 17, 2021 at 11:44

1 Answer 1

1

You can use a a recursive approach, say, creating a function getPaths that will get a list of paths using a property key as an input.

In this case we'll use 'NAME', and build up an array of the relevant paths.

let input = [ { "PRODUCT_CATEGORY_ID": "1", "NAME": "OTOMOBİL PARÇALARI", "DESCRIPTION": "Araba Parçaları", "ACTIVE": "True", "PARENT_PRODUCT_CATEGORY_ID": "", "children": [ { "PRODUCT_CATEGORY_ID": "3", "NAME": "HONDA PARÇALARI", "DESCRIPTION": "Honda Parçaları", "ACTIVE": "True", "PARENT_PRODUCT_CATEGORY_ID": "1" }, { "PRODUCT_CATEGORY_ID": "4", "NAME": "RENAULT PARÇALARI", "DESCRIPTION": "Renault Parçaları", "ACTIVE": "True", "PARENT_PRODUCT_CATEGORY_ID": "1", "children": [ { "PRODUCT_CATEGORY_ID": "5", "NAME": "MINIMAL RENAULT PARÇALARI", "DESCRIPTION": "", "ACTIVE": "True", "PARENT_PRODUCT_CATEGORY_ID": "4" } ] } ] } ]

function getPaths(obj, property, path = '') {
    const paths = [];
    for(let k in obj) {
        if (typeof(obj[k]) === 'object') {
            paths.push(...getPaths(obj[k], property, path));
        } else if (k === property) {
            path += (path ? ' > ' : '') + obj[k];
            paths.push(path)
        }
    }
    return paths;
}

console.log('Paths:', getPaths(input, 'NAME'))

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, for your help. This is so helpful. This answer is working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.