Q:

Flatten an array with nested objects

I have an array with objects, that can have children, the children have the same structure as the parent, it's just object nesting basically.
I'm wondering how I can flatten the structure of my objects so I have the id's of all the objects, including the nested one's. 
For example, This structure
const data = [
  {
    id: 2,
    children: [
      {
        id: 1,
        children: []
      }
    ]
  },
  {
    id: 3,
    children: [],
  }
]

Should be flattened to this
const data = [2,1,3]

I've tried
Using Array.reduce() and the object spread syntax, but I can't wrap my head around the logic required to do this.

A:

const data = [
  {
    id: 2,
    children: [
      {
        id: 1,
        children: []
      }
    ]
  },
  {
    id: 3,
    children: [],
  }
]

const getIds = (data) => data.map(d => [d.id, ...getIds(d.children)]).flat()

console.log(getIds(data))

