RıdvanÖnal
RıdvanÖnal

Reputation: 189

JS Recursive Nested JSON Array Children Join Key

[
  {
    "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.

Upvotes: 0

Views: 965

Answers (1)

Terry Lennox
Terry Lennox

Reputation: 30685

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'))

Upvotes: 1

Related Questions