简体   繁体   English

降低 Typescript/JS 中嵌套 object 数组的级别

[英]Reduce level of nested object array in Typescript/JS

I'm learning TypeScript and Angular and I'm struggling with a problem.我正在学习 TypeScript 和 Angular 并且我正在努力解决一个问题。 Say, I have an array of Application objects.说,我有一个Application对象数组。 Each Application object has this kind of a nested structure:每个Application object 都有这种嵌套结构:

Application应用
| |
----[Document Type] ----[文档类型]
---------| ---------|
--------------[Document] - - - - - - - [文档]
------------------| ------------------|
------------------------Metadata ------------------------元数据

(Each Application has an array of Document Type . Each Document Type has an array of Document . Each Document has a Metadata inside it) (每个Application都有一个Document Type数组。每个Document Type都有一个Document数组。每个Document里面都有一个Metadata

Example:例子:

[
 {
  "name": "Application 1",
  "parent_type": "root",
  "children": [
   {
    "name": "Operations Manual",
    "parent": "Application 1",
    "parent_type": "application",
    "count": 2,
    "children": [
     {
      "name": "App1-OpManual-1",
      "metadata": {
       "actualDocName": "operations1-app1",
       "currentStatus": "for review",
       "lastModifiedBy": "user 1",
       "size": 56,
       "fileExtension": "docx"
      }
     },
     {
      "name": "App1-OpManual-2",
      "metadata": {
       "actualDocName": "operations2-app1",
       "currentStatus": "for review",
       "lastModifiedBy": "user 2",
       "size": 56,
       "fileExtension": "pdf"
      }
     }
    ]
   }
  ]
 }
]

I'm trying to reduce this array to this (basically, to retain the nesting only till level 2 and discard the rest):我正在尝试将此数组减少到这个(基本上,只保留嵌套到第 2 级并丢弃其余的):

Application应用
| |
----[Document Type] ----[文档类型]

..so that, the JSON becomes like this: ..所以,JSON 变成这样:

[
 {
  "name": "Application 1",
  "parent_type": "root",
  "children": [
   {
    "name": "Operations Manual",
    "parent": "Application 1",
    "parent_type": "application",
    "count": 2
   }
  ]
 }
] 

I've been trying with multiple examples from tutorials, but haven't been able to get the correct JS to do this.我一直在尝试使用教程中的多个示例,但无法获得正确的 JS 来执行此操作。 Can anybody please help?有人可以帮忙吗?

You could use .map() and destructuring to grab the properties and values you need from your outer-most objects within your array, and then use .map() again with destructuring to get the properties you need fro your inner-most object to map each application object in your array to a newly created minified version of itself:您可以使用.map()解构从数组中最外层的对象中获取所需的属性和值,然后再次使用.map()和解构从最内层的 object 中获取所需的属性map 阵列中的每个应用程序 object 到新创建的自身缩小版本:

 const arr = [{name:"Application 1",parent_type:"root",children:[{name:"Operations Manual",parent:"Application 1",parent_type:"application",count:2,children:[{name:"App1-OpManual-1",metadata:{actualDocName:"operations1-app1",currentStatus:"for review",lastModifiedBy:"user 1",size:56,fileExtension:"docx"}},{name:"App1-OpManual-2",metadata:{actualDocName:"operations2-app1",currentStatus:"for review",lastModifiedBy:"user 2",size:56,fileExtension:"pdf"}}]}]}]; const res = arr.map(({children, ...r}) => ( {...r, children: children.map(({children, ...keep}) => ({...keep})) } )); console.log(res);

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

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