具有嵌套对象数组

时间:2018-02-02 15:25:32

标签: javascript arrays typescript object

我有以下对象数组

let prova: ActiveRoute[] = [
{
    path: '/Root',
    method: 'GET',
    children: [
        {
            path: '/Son',
            method: 'GET',
            children: [
                {
                    path: '/Grandson',
                    method: 'GET',
                    children: [
                        {
                            path: '/Boh',
                            method: 'GET',
                            activeMessage: 'End',
                        }
                    ],
                }
            ],
        }
    ],
    middleware: [
        'middleware1',
    ],
}

这是ActiveRoute接口

export interface  ActiveRoute {
   path: string;
   children?: ActiveRoute[];
   middleware?: string[];
   method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
   activeMessage?: string;

}

我想在字符串中打印所有路径属性。 我该怎么办?

这就是我所做的(错误的)

function getEndPoints(prova) {
let endpoints: string = '';
prova.forEach((r) => {
    if (r.path) {
        endpoints += r.path;
        if(r.children){
            r.children.forEach((s) => {
                if (s.path) {
                    endpoints += s.path;
                }
                if (s.children){
                    s.children.forEach((z) =>{
                        if (z.path){
                            endpoints += z.path;
                        }
                    });
                }
            });
        }
    }
});
console.log(endpoints);

}

我真的不明白我应该如何在一系列物体中连续而深入地循环。 这是我的愿望输出,在这种情况下:' / Root / Son / Grandson / Boh'。

显然现在我不知道我将如何深入其中。

2 个答案:

答案 0 :(得分:1)

您的输入结构可能有多个结果,..

例如。我在下面进行了修改,以便/Grandson有多个孩子。



let prova = [
{
    path: '/Root',
    method: 'GET',
    children: [
        {
            path: '/Son',
            method: 'GET',
            children: [
                {
                    path: '/Grandson',
                    method: 'GET',
                    children: [
                        {
                            path: '/Boh',
                            method: 'GET',
                            activeMessage: 'End',
                        },
                        {
                            path: '/AnotherBoh',
                            method: 'GET',
                            activeMessage: 'End',
                        }
                    ],
                }
            ],
        }
    ],
    middleware: [
        'middleware1',
    ]
}];

function getLinks(p) {
  const arr = [];
  function inner(p, root) {
    p.forEach((x) => {
      const newroot = root + x.path;
      if (!x.children) {
        arr.push(newroot);
      } else {
        inner(x.children, newroot);
      }
    });
  }
  inner(p, "");
  return arr;
}

console.log(getLinks(prova));




答案 1 :(得分:0)

这是另一种可能性,基于@Keith的答案,但它将路径列表作为字符串数组返回而不是记录它们。

let prova = [{
    path: '/Root',
    children: [{
        path: '/Son',
        children: [{
            path: '/Grandson',
            children: [{
                path: '/Boh',
            }, {
                path: '/AnotherBoh',
                children: [{
                   path: '/Foo'
                }, {
                  path: '/Bar'
                }]
            }]
        }]
    }, {
        path: '/AnotherSon',
    }],
    middleware: ['middleware1']
}];

function getPaths(p, base = "", gather = []) {
  return p.map((node) => {
    if (node.children) {
      getPaths(node.children, base + node.path, gather);
    } else {
      gather.push(base + node.path);
    }
    return gather
  }).reduce((a, b) => a.concat(b), []); // removes an (unnecessary?) level of nesting
}

console.log(getPaths(prova));

请注意,我删除了一些不相关的属性,但在几个级别添加了嵌套,只是为了测试。

更新

以下是相同想法的更清晰版本:

const flat = arr => arr.reduce((out, item) => out.concat(item), [])

const getPaths = (p, base = "", gather = []) => flat(p.map((node) => ('children' in node) 
    ? getPaths(node.children, base + node.path, gather)
    : gather.concat(base + node.path)
))