const arr = [...] const condition: Record = { 2: [], 3: [31, 33] } const r = arr.reduce((p, c) => { if (c.id in condition) { if (c.children) { const [a, b] = c.children.reduce( ( [a, b], cc, ) => (condition[c.id].includes(cc.id) ? [a, [...b, cc]] : [[...a, cc], b]), [[], []] as typeof arr[], ) if (a.length) p[0].push({ ...c, children: a }) p[1].push({ ...c, children: b }) } else p[1].push(c) } else p[0].push(c) return p }, [[], []] as typeof arr[]) console.log(r)