-1

How can I find duplicate and remove object have id: 3 and just keep id: 3 in children of id: 2 ?

I have one array object like this:

const data = [
  {
    "id": 1,
    "name": ABC,
    "parentID": null,
    "children": [
      {
         "id": 2,
         "name": ASD,
         "parentID": 1,
         "children": [
            {
              "id": 3,
              "name": AS,
              "parentID": 2,
              "children": []
            }
          ]
      }
    ]
  },
  {
    "id": 3,
    "name": AS,
    "parentID": 2,
    "children": []
  },
  {
    "id": 4,
    "name": ASC,
    "parentID": null,
    "children": []
  }
]

I want to write a function for search and remove object id 3, Can I do that by recursive ?

Output:

const data = [
  {
    "id": 1,
    "name": ABC,
    "parentID": null,
    "children": [
      {
         "id": 2,
         "name": ASD,
         "parentID": 1,
         "children": [
            {
              "id": 3,
              "name": AS,
              "parentID": 2,
              "children": []
            }
          ]
      }
    ]
  },
  {
    "id": 4,
    "name": ASC,
    "parentID": null,
    "children": []
  }
]

Do you any idea for this issues ?

Thanks for your help

1 Answers1

1

A simple iterative recursion should resolve this problem. You have to store a list of unique ids that represents each item in the data tree, and use these keys to check if such an item has been traversed or not.

type DataType = {
    id: number;
    name: string;
    parentID: number | null;
    children: DataType[];
};

function filterRecursive(collection: DataType[], keys: Record<number, true> = {}) {
    const result: DataType[] = [];

    for(const item of collection) {
        if(item.id in keys === false) {
            keys[item.id] = true;
            result.push({
                ...item,
                children: filterRecursive(item.children, keys)
            });
        }
    }

    return result;
}

const result = filterRecursive(data);

.as-console-wrapper {
  top: 0 !important;
  max-height: 100% !important;
}
<script src="https://unpkg.com/@babel/standalone@7.18.13/babel.min.js"></script>
<script type="text/babel" data-type="module" data-presets="typescript">

type DataType = {
    id: number;
    name: string;
    parentID: number | null;
    children: DataType[];
};

const data = [
  {
    "id": 1,
    "name": 'ABC',
    "parentID": null,
    "children": [
      {
         "id": 2,
         "name": 'ASD',
         "parentID": 1,
         "children": [
            {
              "id": 3,
              "name": 'AS',
              "parentID": 2,
              "children": []
            }
          ]
      }
    ]
  },
  {
    "id": 3,
    "name": 'AS',
    "parentID": 2,
    "children": []
  },
  {
    "id": 4,
    "name": 'ASC',
    "parentID": null,
    "children": []
  }
];

function filterRecursive(collection: DataType[], keys: Record<number, true> = {}) {
    const result: DataType[] = [];

    for(const item of collection) {
        if(item.id in keys === false) {
            keys[item.id] = true;
            result.push({
                ...item,
                children: filterRecursive(item.children, keys)
            });
        }
    }

    return result;
}

const result = filterRecursive(data);

console.log(JSON.stringify(result, null, 4));


</script>
ryeballar
  • 29,658
  • 10
  • 65
  • 74