最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get nested parent path from a JSON tree in JavaScript? - Stack Overflow

programmeradmin2浏览0评论

I have a JSON tree structure like this.

[
    {
      "title":"News",
      "id":"news"
    },
    {
      "title":"Links",
      "id":"links",
      "children":[
        {
          "title":"World",
          "id":"world",
          "children":[
            {
              "title":"USA",
              "id":"usa",
              "children":[
                {
                  "title":"Northeast",
                  "id":"northeast"
                },
                {
                  "title":"Midwest",
                  "id":"midwest"
                }                
              ]
            },
            {
              "title":"Europe",
              "id":"europe"
            }
          ]
        }
      ]
    }
  ]

What i want is when i pass "northeast" to a function() it should return me the dot notation string path from the root. Expected return string from the function in this case would be "links.world.usa.northeast"

I have a JSON tree structure like this.

[
    {
      "title":"News",
      "id":"news"
    },
    {
      "title":"Links",
      "id":"links",
      "children":[
        {
          "title":"World",
          "id":"world",
          "children":[
            {
              "title":"USA",
              "id":"usa",
              "children":[
                {
                  "title":"Northeast",
                  "id":"northeast"
                },
                {
                  "title":"Midwest",
                  "id":"midwest"
                }                
              ]
            },
            {
              "title":"Europe",
              "id":"europe"
            }
          ]
        }
      ]
    }
  ]

What i want is when i pass "northeast" to a function() it should return me the dot notation string path from the root. Expected return string from the function in this case would be "links.world.usa.northeast"

Share edited Jan 13, 2020 at 8:54 Eddie 26.9k6 gold badges38 silver badges59 bronze badges asked Jan 13, 2020 at 8:54 Sakti DashSakti Dash 751 silver badge5 bronze badges 5
  • please post your effort (code) to achieve this. – Ahmed Ali Commented Jan 13, 2020 at 8:57
  • nested parent path? you'll need to explain what you mean by that – Jaromanda X Commented Jan 13, 2020 at 8:58
  • Objects are not aware of their "parents", since objects don't have a parent. You've to mark the parent as a property in the objects having a "parent". – Teemu Commented Jan 13, 2020 at 9:00
  • Are you trying to implement a binary tree? – arizafar Commented Jan 13, 2020 at 9:01
  • Please add the code you've tried. There are many similar questions: Get parent and grandparent keys from a value inside a deep nested object and Javascript - Find path to object reference in nested object and Find a full object path to a given value with JavaScript – adiga Commented Jan 13, 2020 at 9:05
Add a ment  | 

1 Answer 1

Reset to default 13

You could test each nested array and if found, take the id from every level as path.

const pathTo = (array, target) => {
    var result;
    array.some(({ id, children = [] }) => {
        if (id === target) return result = id;
        var temp = pathTo(children, target)
        if (temp) return result = id + '.' + temp;
    });
    return result;
};

var data = [{ title: "News", id: "news" }, { title: "Links", id: "links", children: [{ title: "World", id: "world", children: [{ title: "USA", id: "usa", children: [{ title: "Northeast", id: "northeast" }, { title: "Midwest", id: "midwest" }] }, { title: "Europe", id: "europe" }] }] }];

console.log(pathTo(data, 'northeast'));

发布评论

评论列表(0)

  1. 暂无评论