简体   繁体   中英

Node JS Nested Array Transformation

I am trying to transform following array but it's only transforming single level. multiple remain same want to remove entity for each level of object.

I have used the map to modify object not sure if there are others method for the multi level

Here is the array


const source = [
  {
    "block_id": 1,
    "entity": 100,
    "block_name": "test 1",
    "children": [
      {
        "block_id": 2,
        "entity": 101,
        "block_name": "test 2",
        "children": [
          {
            "block_id": 3,
             "entity": 105,
            "block_name": "test 2",
          }

        ]
      }
      
    ],
  }
]

Tried following code to transform

function trans(item) {
  const items = Array.isArray(item) ? item : [item];
    return items.map( t => {
        return { block_id: t.block_id, block_name: t.block_name, children: t.children };
    });
}

I am getting following

Output

[
  {
    "block_id": 1,
    "block_name": "test 1",
    "children": [
      {
        "block_id": 2,
        "entity": 101,
        "block_name": "test 2",
        "children": [
          {
            "block_id": 3,
            "entity": 105,
            "block_name": "test 2",
          }

        ]
      }
      
    ],
  }
]

Expected

[
  {
    "block_id": 1,
    "block_name": "test 1",
    "children": [
      {
        "block_id": 2,
        "block_name": "test 2",
        "children": [
          {
            "block_id": 3,
            "block_name": "test 2",
          }

        ]
      }
      
    ],
  }
]

Please help

Wanted result can be implemented easily with recursion :

 const source = [ { "block_id": 1, "entity": 100, "block_name": "test 1", "children": [ { "block_id": 2, "entity": 101, "block_name": "test 2", "children": [ { "block_id": 3, "entity": 105, "block_name": "test 2", } ] } ], }]; const transform=arr=>arr.map(({entity,...rest})=> rest.children? ({...rest, children: transform(rest.children)}): rest); console.log(transform(source));

if you don't mind to change your origin Array,you can try this codes:

 function trans(item, element) { const items = Array.isArray(item)? item: [item]; items.forEach(t => { if (typeof t === 'object') { if (element in t) { delete t[element] } for (const argument in t) { const item = t[argument] if (typeof item === 'object') { trans(item,element) } } } }); }

and the result is:

 const source = [ { "block_id": 1, "entity": 100, "block_name": "test 1", "children": [ { "block_id": 2, "entity": 101, "block_name": "test 2", "children": [ { "block_id": 3, "entity": 105, "block_name": "test 2", } ] } ], } ] function trans(item, element) { const items = Array.isArray(item)? item: [item]; items.forEach(t => { if (typeof t === 'object') { if (element in t) { delete t[element] } for (const argument in t) { const item = t[argument] if (typeof item === 'object') { trans(item, element) } } } }); } trans(source, 'entity') console.log(JSON.stringify(source, null, 2));

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