0

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

1
  • You can do it well using recurison. Commented Feb 18, 2021 at 9:52

2 Answers 2

2

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));

Sign up to request clarification or add additional context in comments.

Comments

1

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.