Transform array into an object using an attribute as a reference

I need to convert an Array into an object, but I have no idea how to do this.

When I receive data from my API, it comes in the following structure:

[
  {
    id: 1,
    identifier: '0'
  },
  {
    id: 4,
    identifier: '0,1,0'
  },
  {
    id: 3,
    identifier: '0,1'
  },
  {
    id: 2,
    identifier: '0,0'
  }
]

After receiving the data I need to execute a method to convert them to the following structure based on the identifier attribute:

{
  id: 1,
  children: [
    {
      id: 2
    },
    {
      id: 3,
      children: [
        {
          id: 4
        }
      ]
    }
  ]
}

Any idea how I can get the desired result?