简体   繁体   中英

How to create data for Flat Array for Angular Tree using Javascript/Typescript

I have the following JSON data ->

{
  "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 .

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

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

The data will always be till level 1 only.

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

 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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