首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从json字符串递归地获取父ids

如何从json字符串递归地获取父ids
EN

Stack Overflow用户
提问于 2020-02-19 07:36:55
回答 5查看 653关注 0票数 0
代码语言:javascript
复制
{
  "id": "1",
  "name": "root",
  "children": [
    {
      "id": "1.1",
      "name": "Child 1",
      "children": [
        {
          "id": "1.1.1",
          "name": "Child 1-1",
          "children": [
            {
              "id": "1-1-1",
              "name": "Child 1-1-1",
              "children": [

              ]
            }
          ]
        },
        {
          "id": "1.1.2",
          "name": "Child 1-2",
          "children": [

          ]
        },
        {
          "id": "1.1.3",
          "name": "Child 1-3",
          "children": [

          ]
        }
      ]
    },
    {
      "id": "1.2",
      "name": "Child 2",
      "children": [
        {
          "id": "1.2.1",
          "name": "Child 2-2",
          "children": [

          ]
        }
      ]
    }
  ]
}

这是作为响应的JSON字符串。必须递归地获取父元素的所有父id。

例如,输入为1.2.1,然后返回1.2,输入为1.1.3,然后返回1.1,1

我怎样才能做到这一点?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2020-02-24 14:48:21

您可以采用迭代和递归方法,并且

如果给定的数据不是对象,则返回

  • chcek,然后返回id
  • 检查,如果找到,则返回一个空数组,该数组由调用的
  • children填充,查找后有一个短路,然后再次调用该函数。

代码语言:javascript
复制
function getParentIds(object, id) {
    var ids;
    if (!object || typeof object !== 'object') return;          // no object
    if (object.id === id) return [];                            // id found
    return object.children.some(o => ids = getParentIds(o, id)) // call recursive function
        ? [...ids, object.id]                                   // if found, take ids
        : undefined;                                            // otherwise return falsy
}

var data = { id: "1", name: "root", children: [{ id: "1.1", name: "Child 1", children: [{ id: "1.1.1", name: "Child 1-1", children: [{ id: "1-1-1", name: "Child 1-1-1", children: [] }] }, { id: "1.1.2", name: "Child 1-2", children: [] }, { id: "1.1.3", name: "Child 1-3", children: [] }] }, { id: "1.2", name: "Child 2", children: [{ id: "1.2.1", name: "Child 2-2", children: [] }] }] };

console.log(getParentIds(data, '1.2.1')); // ['1.2']
console.log(getParentIds(data, '1.1.3')); // ['1.1', '1']
console.log(getParentIds(data, 'foo'));   // undefined
代码语言:javascript
复制
.as-console-wrapper { max-height: 100% !important; top: 0; }

票数 1
EN

Stack Overflow用户

发布于 2020-02-23 06:51:34

使用dfs的简单递归

代码语言:javascript
复制
const data = JSON.parse('{"id":"1","name":"root","children":[{"id":"1.1","name":"Child 1","children":[{"id":"1.1.1","name":"Child 1-1","children":[{"id":"1-1-1","name":"Child 1-1-1","children":[]}]},{"id":"1.1.2","name":"Child 1-2","children":[]},{"id":"1.1.3","name":"Child 1-3","children":[]}]},{"id":"1.2","name":"Child 2","children":[{"id":"1.2.1","name":"Child 2-2","children":[]}]}]}')

function dfs (target, node) {
  if (node.id === target) { return node.id }
  if (!node.children) { return false } // we could even skip that line since in your data you seem to have an empty array
  const has = node.children.find(c => dfs(target, c))
  return has && [node.id].concat(has.id)
}
console.log(dfs('1.2.1', data))
console.log(dfs('1.1.3', data))

票数 2
EN

Stack Overflow用户

发布于 2020-02-19 08:36:24

您必须解析完整的对象,以便递归地在子对象中注入父in。像这样的东西就行了。

代码语言:javascript
复制
function injectParents(data, parents) {
    if(!parents) {
        parents = [];
    }
    parents.push(data.id);
    data.children.map(child=>{
        child.parents = parents;
        if(child.children && child.children.length>0) {
            child = injectParents(child, Array.from(parents));
        }
        return child;
        });
    return data;    
}

你通常会把它叫做

代码语言:javascript
复制
const injectedResponse = injectParents(response, null);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60295173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档