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.

RıdvanÖnal
  • 159
  • 1
  • 2
  • 8
  • 1
    _"I have a json object..."_ - No, because [there's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). What you have there is an array with one object. – Andreas Sep 17 '21 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? – Andreas Sep 17 '21 at 10:33
  • 2
    [Recursively list all object property paths from JSON](https://stackoverflow.com/questions/30286814/recursively-list-all-object-property-paths-from-json) – Andreas Sep 17 '21 at 10:37
  • Thank you for this link. But this json is not same my json so. It didn't solve the problem. – RıdvanÖnal Sep 17 '21 at 11:06
  • 1
    Then make the necessary adjustments so it fits your object (there's still no [JSON](https://www.json.org/json-en.html) 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... – Andreas Sep 17 '21 at 11:44

1 Answers1

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'))
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40