Fetching the childrens by id from the json tree structure data

I am using JS to archive the following functionality.

My json data

data=[{"id":63,"parent":59,"currency":1},{"id":64,"parent":59,"currency":1},
{"id":1,"parent":null,"currency":1},{"id":2,"parent":1,"currency":4},
{"id":4,"parent":3,"currency":4},{"id":3,"parent":2,"currency":2},
{"id":5,"parent":1,"currency":5},{"id":6,"parent":1,"currency":6},
{"id":7,"parent":3,"currency":2},{"id":8,"parent":1,"currency":1},
{"id":57,"parent":56,"currency":1},{"id":58,"parent":1,"currency":1},
{"id":59,"parent":1,"currency":1},{"id":30,"parent":1,"currency":1},
{"id":31,"parent":1,"currency":1},{"id":32,"parent":1,"currency":1},
{"id":33,"parent":1,"currency":1},{"id":34,"parent":1,"currency":1},
{"id":35,"parent":1,"currency":1},{"id":36,"parent":1,"currency":18},
{"id":37,"parent":1,"currency":1},{"id":39,"parent":1,"currency":1},
{"id":41,"parent":6,"currency":6},{"id":42,"parent":1,"currency":1},
{"id":43,"parent":42,"currency":1},{"id":48,"parent":47,"currency":2},
{"id":47,"parent":37,"currency":2},{"id":49,"parent":48,"currency":2},
{"id":52,"parent":1,"currency":16},{"id":9,"parent":1,"currency":9},
{"id":54,"parent":1,"currency":4},{"id":55,"parent":1,"currency":1},
{"id":56,"parent":1,"currency":1},{"id":19,"parent":1,"currency":12},
{"id":21,"parent":1,"currency":1},{"id":12,"parent":1,"currency":1},
{"id":13,"parent":1,"currency":1},{"id":15,"parent":1,"currency":1},
{"id":16,"parent":1,"currency":1},{"id":10,"parent":1,"currency":8},
{"id":11,"parent":1,"currency":4},{"id":66,"parent":1,"currency":1},
{"id":69,"parent":68,"currency":1},{"id":65,"parent":1,"currency":1},
{"id":68,"parent":1,"currency":1},{"id":70,"parent":1,"currency":11}]

What I am trying to archive:

  1. The Js code should fetch all the “ids” of the children and grandchildren of the inputted “id” and form as new json key(children).
  2. If there is no children found then add their own “id” in children array

if my input is “id” = 59

my resulted json data:

resulteddata = [{"id":59,"currency":1,"children":[59,63,64]}

if my input is “id” = 1

my resulted json data:

resulteddata = [{"id":1,"currency":1,"children":[1,2,3,4,5,6,7,8,9 ...... 70]} // all the children and grand children

Thanks in advance

Answer

data = [{"id":63,"parent":59,"currency":1},{"id":64,"parent":59,"currency":1},{"id":1,"parent":null,"currency":1},{"id":2,"parent":1,"currency":4},{"id":4,"parent":3,"currency":4},{"id":3,"parent":2,"currency":2},{"id":5,"parent":1,"currency":5},{"id":6,"parent":1,"currency":6},{"id":7,"parent":3,"currency":2},{"id":8,"parent":1,"currency":1},{"id":57,"parent":56,"currency":1},{"id":58,"parent":1,"currency":1},{"id":59,"parent":1,"currency":1},{"id":30,"parent":1,"currency":1},{"id":31,"parent":1,"currency":1},{"id":32,"parent":1,"currency":1},{"id":33,"parent":1,"currency":1},{"id":34,"parent":1,"currency":1},{"id":35,"parent":1,"currency":1},{"id":36,"parent":1,"currency":18},{"id":37,"parent":1,"currency":1},{"id":39,"parent":1,"currency":1},{"id":41,"parent":6,"currency":6},{"id":42,"parent":1,"currency":1},{"id":43,"parent":42,"currency":1},{"id":48,"parent":47,"currency":2},{"id":47,"parent":37,"currency":2},{"id":49,"parent":48,"currency":2},{"id":52,"parent":1,"currency":16},{"id":9,"parent":1,"currency":9},{"id":54,"parent":1,"currency":4},{"id":55,"parent":1,"currency":1},{"id":56,"parent":1,"currency":1},{"id":19,"parent":1,"currency":12},{"id":21,"parent":1,"currency":1},{"id":12,"parent":1,"currency":1},{"id":13,"parent":1,"currency":1},{"id":15,"parent":1,"currency":1},{"id":16,"parent":1,"currency":1},{"id":10,"parent":1,"currency":8},{"id":11,"parent":1,"currency":4},{"id":66,"parent":1,"currency":1},{"id":69,"parent":68,"currency":1},{"id":65,"parent":1,"currency":1},{"id":68,"parent":1,"currency":1},{"id":70,"parent":1,"currency":11}]

function children(id, data) {
    return data.filter(e => e.parent === id).flatMap(e => [e.id, ...children(e.id, data)])
}

function find(id, data) {
    return {
        ...data.find(e => e.id === id),
        children: children(id, data).sort((a, b) => a === b ? 0 : a > b ? 1 : -1)
    };
}

console.log(find(1, data))