繁体   English   中英

具有嵌套对象数组的对象数组

[英]Array of objects with nested array of objects

我有以下对象的数组

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

显然,现在我不怎么深入自己了。

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

例如。 下面,我已经修改,以便/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)); 

这是基于@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)
))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM