[英]How to create data for Flat Array for Angular Tree using Javascript/Typescript
I have the following JSON data ->我有以下 JSON 数据 ->
{
"data": [
{
"documentId": "new_148_45646",
"data": "new_data6"
},
{
"documentId": "new_145_456",
"data": "new_data1"
},
{
"documentId": "new_148_4546",
"data": "new_data2"
},
{
"documentId": "new_145_456",
"data": "new_data3"
},
{
"documentId": "new_148_4546",
"data": "new_data6"
}
]
}
I want to convert this to following flat tree data我想将其转换为以下平面树数据
{
"treeData": [
{
"documentId": "new_148_45646",
"expandable": true,
"level": 0
}, {
"documentId": "new_148_45646",
"expandable": false,
"level": 1,
"data": "new_data6"
},
{
"documentId": "new_145_456",
"expandable": true,
"level": 0
}, {
"documentId": "new_145_456",
"expandable": false,
"level": 1,
"data": "new_data1"
},
{
"documentId": "new_145_456",
"expandable": false,
"level": 1,
"data": "new_data3"
},
{
"documentId": "new_148_4546",
"expandable": true,
"level": 0
}, {
"documentId": "new_148_4546",
"expandable": false,
"level": 1,
"data": "new_data2"
},
{
"documentId": "new_148_4546",
"expandable": false,
"level": 1,
"data": "new_data6"
}
]
}
So if there is a single element with a unique documentId in the original array then it will be converted to 2 nodes -> One node with the documentid , level 0 ,expandable=true and another node node with same documentid, expandable=false, level 1, data .因此,如果原始数组中有一个具有唯一 documentId 的单个元素,那么它将被转换为 2 个节点 -> 一个具有 documentid 、级别 0 、expandable=true 的节点和具有相同 documentid、expandable=false、级别的另一个节点节点1、数据。
If there are 2 elements with a same documentId in the original array then it will be converted to 3 nodes -> One node with the documentid , level 0 ,expandable=true and 2 nodes with same documentid, expandable=false, level 1, data如果原始数组中有 2 个具有相同 documentId 的元素,那么它将被转换为 3 个节点 -> 一个具有 documentid 、级别 0 、expandable=true 的节点和具有相同 documentid、expandable=false、级别 1、数据的 2 个节点
If there are 3 elements with a same documentId in the original array then it will be converted to 4 nodes -> One node with the documentid , level 0 ,expandable=true and 3 nodes with same documentid, expandable=false, level 1, data如果原始数组中有 3 个具有相同 documentId 的元素,那么它将被转换为 4 个节点 -> 一个具有 documentid 、级别 0 、expandable=true 的节点和具有相同 documentid、expandable=false、级别 1、数据的 3 个节点
The data will always be till level 1 only.数据将始终只保留到级别 1。
Could some one please help me with this.有人可以帮我解决这个问题。 Thanks
谢谢
probably not the best code, but it is a one-liner - so that's better than no lines :p可能不是最好的代码,但它是单行代码 - 所以总比没有行好:p
const input = { "data": [ { "documentId": "new_148_45646", "data": "new_data6" }, { "documentId": "new_145_456", "data": "new_data1" }, { "documentId": "new_148_4546", "data": "new_data2" }, { "documentId": "new_145_456", "data": "new_data3" }, { "documentId": "new_148_4546", "data": "new_data6" } ] } const output = [...input.data.reduce((a,{documentId, data})=>(a.set(documentId,(a.get(documentId)||[]).concat([{documentId,expandable:false,level:1,data,}])),a),new Map)].flat().flatMap(i=>typeof i==='string'?({documentId:i,expandable:true,level:0}):i); console.log(output)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.