简体   繁体   English

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

[英]JavaScript Objects - I'm having issues accessing an array of objects properties nested multiple levels & combining them together

I have an array of objects & those objects contain a property called children which are also objects & they repeat them multiple levels deep(or not).我有一个objects array ,这些objects包含一个名为children的属性,这些属性也是objects ,并且它们在多个级别(或不)重复它们。

Basically, I need to accomplish two things :基本上,我需要完成两件事

  • Get the first level path property's value & its multiple level within the children property.children属性中获取第一级path属性的值及其多个级别。

  • Concatenating them together to get single string which will be a route/URL.将它们连接在一起以获得将是路由/URL 的单个string

For example, following the sample code below, I'll need to get three differents URLs/routes like so:例如,按照下面的示例代码,我需要获得三个不同的 URL/路由,如下所示:

/system-settings1/accounting1/accounting1/etc

/system-settings2/accounting2/accounting2/etc

/system-settings3/accounting3/accounting3/etc

This is small sample of my object :这是我的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)
      }
    }
  },
]

I was able to get the parent's path & its next immediate children's path like so:我能够像这样获得父路径及其下一个直接子路径:

const routeParentPaths = routes.map(({path}) => path);

const routeChildrenPaths = routes.map(({children}) => children.path);

console.log((routeParentPaths.concat(routeChildrenPaths)));

But I need to find a way to access all its children's paths plus I need to find a way to concatenate in order together to form a proper URL per each object .但是我需要找到一种方法来访问其所有子路径,而且我需要找到一种方法来连接以便为每个object形成一个正确的 URL。

You find a code sample here .您可以在此处找到代码示例

You could create a recursive function which takes an object as parameter.您可以创建一个将对象作为参数的递归函数。 Destructure the parameter to get path and children property. 解构参数以获取pathchildren属性。 If the nested object doesn't have children , return just path .如果嵌套对象没有children ,则只返回path Else, recursively call the function on children and append it to the current path否则,递归调用该函数children ,并将其附加到当前path

Then, call this function on each item in the routes array using map然后,使用maproutes数组中的每个项目调用此函数

const getPath = ({ path, children }) => children ? `${path}/${getPath(children)}` : path

const output = routes.map(getPath)

Here's a snippet:这是一个片段:

 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"}}},{path:"/system-settings2",name:"global-settings2",component:"tabs2",children:{path:"accounting2",name:"local-settings2",components:"modals2",children:{path:"accounting1",name:"local-settings1",components:"modals1"}}},{path:"/system-settings3",name:"global-settings3",component:"tabs3",children:{path:"accounting3",name:"local-settings3",components:"modals3",children:{path:"accounting1",name:"local-settings1",components:"modals1"}}},], getPath = ({ path, children }) => children ? `${path}/${getPath(children)}` : path, output = routes.map(getPath) console.log(output)

So you can simply keep checking if the children key exists and update the object you are checking against.所以你可以简单地继续检查children键是否存在并更新你正在检查的对象。 This can get as nested as you would like.这可以按照您的意愿进行嵌套。 I updated one of your routes to allow this to be shown more easily.我更新了您的一条路线,以便更轻松地显示这一点。

Basically基本上

 const routes = [ { path: '/system-settings1', name: 'global-settings1', component: 'tabs1', children: { path: 'CHILD1accounting1', name: 'local-settings1', components: 'modals1', children: { path: 'CHILD2accounting1', 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) } } }, ] let x = routes.map(route => { let path = route.path + "/"; while(route.hasOwnProperty('children')) { route = route.children; path += `${route.path}/` } return path; }); console.log(x);

Iterate the routes array with Array.map() , and use a do...while loop to construct a url from a nested object.使用Array.map()迭代routes数组,并使用do...while循环从嵌套对象构造 url。

 const getUrl = route => { let r = route let url = [] do { url.push(r.path) } while(r = r.children) return url.join('/') } const getUrls = routes => routes.map(getUrl) 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"}}},{"path":"/system-settings2","name":"global-settings2","component":"tabs2","children":{"path":"accounting2","name":"local-settings2","components":"modals2","children":{"path":"accounting1","name":"local-settings1","components":"modals1"}}},{"path":"/system-settings3","name":"global-settings3","component":"tabs3","children":{"path":"accounting3","name":"local-settings3","components":"modals3","children":{"path":"accounting1","name":"local-settings1","components":"modals1"}}}] const result = getUrls(routes) console.log(result)

You can create recursive function that will receive data object and previous path and if the object has children call it again.您可以创建递归函数,该函数将接收数据对象和以前的路径,如果该对象有子对象,则再次调用它。

 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"}}},{"path":"/system-settings2","name":"global-settings2","component":"tabs2","children":{"path":"accounting2","name":"local-settings2","components":"modals2","children":{"path":"accounting1","name":"local-settings1","components":"modals1"}}},{"path":"/system-settings3","name":"global-settings3","component":"tabs3","children":{"path":"accounting3","name":"local-settings3","components":"modals3","children":{"path":"accounting1","name":"local-settings1","components":"modals1"}}}] function toPaths(data, prev = '') { const result = [] function build(obj, prev = '') { prev += (prev ? '/' : '') + obj.path; if (obj.children) build(obj.children, prev) else result.push(prev) } data.forEach(e => build(e)); return result; } const paths = toPaths(routes) console.log(paths)

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

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