Home > Software engineering >  How can I check duplicate and remove object in array object?
How can I check duplicate and remove object in array object?

Time:09-28

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

  • Related