JavaScript对象-访问嵌套多个级别并将它们组合在一起的对象属性数组时遇到问题

问题描述 投票:0回答:2

我的arrayobjects,并且那些objects包含一个称为children的属性,它们也为[[objects,并且重复它们多深(或不深)。

基本上,

我需要完成两件事

  • 获取第一级

    path

属性的值及其在[[children属性中的多个级别。将它们连接在一起以获得单个
  • string
  • ,这将是一个路由/ URL。

    例如,按照下面的示例代码,我需要像这样获得三种不同的URL /路由:

    [/system-settings1/accounting1/accounting1/etc

    ] >>/system-settings2/accounting2/accounting2/etc

    /system-settings3/accounting3/accounting3/etc

    这是我的object

    的小样本:

    const routes = [ { path: '/system-settings1', name: 'global-settings1', component: 'tabs1', children: { path: 'accounting1', name: 'local-settings1', components: 'modals1', children: { path: 'accounting1', name: 'local-settings1', components: 'modals1' // more children deeply nested(or not) } } }, { path: '/system-settings2', name: 'global-settings2', component: 'tabs2', children: { path: 'accounting2', name: 'local-settings2', components: 'modals2', children: { path: 'accounting1', name: 'local-settings1', components: 'modals1' // more children deeply nested(or not) } } }, { path: '/system-settings3', name: 'global-settings3', component: 'tabs3', children: { path: 'accounting3', name: 'local-settings3', components: 'modals3', children: { path: 'accounting1', name: 'local-settings1', components: 'modals1' // more children deeply nested(or not) } } }, ]

    我能够像这样获得父级的路径及其下一个直接子级的路径:
    const routeParentPaths = routes.map(({path}) => path);
    
    const routeChildrenPaths = routes.map(({children}) => children.path);
    
    console.log((routeParentPaths.concat(routeChildrenPaths)));
    

    但是我需要找到一种方法来访问其所有子级路径,还要找到一种连接方法,以便每个

    object
    一起形成一个正确的URL。

    您找到一个代码示例here

    我有一个对象数组,这些对象包含一个称为子代的属性,它们也是对象,并且它们在深度上(或不)重复多个级别。基本上,我需要完成两个...

    javascript arrays object url routes
    2个回答
    0
    投票

    0
    投票
    © www.soinside.com 2019 - 2024. All rights reserved.