JavaScript
de:
const array = [ { cantDelete: false, cantShifLeft: true, cantShiftDown: false, cantShiftRight: true, cantShiftUp: true, children: ["g12034c6b772_0_5", "g12034c6b772_0_25", "g12034c6b772_0_15"], displayId: "1", id: "533c1e97-465e-4f3e-827b-3b6db8e745f4", idx: 0, kind: "folder", order: 1, parent: null, title: "test" }, { id: "g12034c6b772_0_5", kind: "slide", notes: "", order: 1, render: {}, title: "Slide 1", type: "snapshot", videos: [] }, { id: "g12034c6b772_0_25", kind: "slide", notes: "", order: 2, render: {}, title: "Slide 2", type: "snapshot", videos: [] }, { id: "g12034c6b772_0_15", kind: "slide", notes: "", order: 3, render: {}, title: "Slide 3", type: "snapshot", videos: [] }, { id: "g12034c6b772_0_35", kind: "slide", notes: "", order: 1, render: {}, title: "Slide 1", type: "snapshot", videos: [] } ];a:
const array = [ { cantDelete: false, cantShifLeft: true, cantShiftDown: false, cantShiftRight: true, cantShiftUp: true, displayId: "1", id: "533c1e97-465e-4f3e-827b-3b6db8e745f4", idx: 0, kind: "folder", order: 1, parent: null, title: "test" children: [{object}, {object}, {object}], }, { id: "g12034c6b772_0_35", kind: "slide", notes: "", order: 1, render: {}, title: "Slide 1", type: "snapshot", videos: [] } ];Después de crear la función de ayuda para encontrar el objeto y separarlo de la matriz, podemos iterar la matriz, empujando a sus hijos a los hijos reales que coinciden con la identificación. Basta con mirar el código.
const array = [{ cantDelete: false, cantShifLeft: true, cantShiftDown: false, cantShiftRight: true, cantShiftUp: true, children: ["g12034c6b772_0_5", "g12034c6b772_0_25", "g12034c6b772_0_15"], displayId: "1", id: "533c1e97-465e-4f3e-827b-3b6db8e745f4", idx: 0, kind: "folder", order: 1, parent: null, title: "test" }, { id: "g12034c6b772_0_5", kind: "slide", notes: "", order: 1, render: {}, title: "Slide 1", type: "snapshot", videos: [] }, { id: "g12034c6b772_0_25", kind: "slide", notes: "", order: 2, render: {}, title: "Slide 2", type: "snapshot", videos: [] }, { id: "g12034c6b772_0_15", kind: "slide", notes: "", order: 3, render: {}, title: "Slide 3", type: "snapshot", videos: [] }, { id: "g12034c6b772_0_35", kind: "slide", notes: "", order: 1, render: {}, title: "Slide 1", type: "snapshot", videos: [] } ]; function find(array, id) { return array.find((obj) => obj.id == id); } function extract(array, id) { var obj = find(array, id); array.splice(array.indexOf(obj), 1); return obj; } function compact(array) { array.forEach(function(item) { if (item.kind == "folder") { var children = []; item.children.forEach(function(id) { children.push(extract(array, id)); }) item.children = children; } }) return array; } console.log(compact(array)) .as-console-wrapper { max-height: 100% !important; }