let fooArray = [
{ foo: "a", indent: 0 },
{ foo: "b", indent: 1 },
{ foo: "c", indent: 2 },
{ foo: "d", indent: 1 },
{ foo: "e", indent: 2 },
{ foo: "e1", indent: 1 },
{ foo: "e2", indent: 0 },
{ foo: "e3", indent: 1 },
{ foo: "f", indent: 2 },
{ foo: "g", indent: 1 },
{ foo: "h", indent: 0 },
];
// let barArray = fooArray.slice().flatMap((e,i,a) => i ? null : Array(a.filter(f => !f.indent).length).fill().map((f,j,b) => a.splice(0,Math.max(a.slice(1).findIndex(g => g.indent === 0)+1,1))));
// WIP // let barArray = fooArray.slice().flatMap((e,i,a) => i ? null : Array(a.filter(f => !f.indent).length).fill().map((f,j,b) => a.splice(0,Math.max(a.slice(1).findIndex(g => g.indent === 0)+1,1)))).map(e => e.map((f,i,a) => a[i+1]?.indent > f.indent ? { foo: f.foo, bar: a.splice(i+1,i+2) } : f).filter(f => f));
// WIP #2 // let barArray = fooArray.slice().map(e=>({...e})).flatMap((e,i,a) => i ? null : Array(a.filter(f => !f.indent).length).fill().map((f,j,b) => a.splice(0,Math.max(a.slice(1).findIndex(g => g.indent === 0)+1,1)))).map(e => e.reverse().map((f,i,a) => /*console.log(`a[i]: ${JSON.stringify(a[i])}\nf: ${JSON.stringify(f)}`) ||*/ a[i+1]?.indent < f.indent ? (a[i+1].bar = [{foo:f.foo}], a.splice(i,i+1)) : {foo:f.foo}).filter(f=>f).reverse());
// WIP #3 // let barArray = fooArray.slice().map(e=>({...e})).flatMap((_,i,a) => i ? null : Array(a.filter(e => !e.indent).length).fill().map(e => a.splice(0,Math.max(a.slice(1).findIndex(f => f.indent === 0)+1,1)))).map(e => e.length > 1 ? e.flatMap((_,i) => i ? null : Array(e.filter(e => !e.indent).length).fill().map(f => e.splice(0,Math.max(e.slice(1).findIndex(g => g.indent === 0)+1,1)))) : e);
// simple get object // let barArray = fooArray.slice().map(e=>({...e})).flatMap((_,i,a) => i ? null : Array(a.filter(e => !e.indent).length).fill().map(e => a.splice(0,Math.max(a.slice(1).findIndex(f => f.indent === 0)+1,1)));
// NEW WIP
fooArray.slice().map(e=>({...e})).flatMap((_,i,a) => i ? null : Array(a.filter(e => !e.indent).length).fill().map(e => a.splice(0,Math.max(a.slice(1).findIndex(f => f.indent === 0)+1,1)))).map(e => e.length > 1 ? { foo: e[0].foo, bar: e.slice(1) } : e[0])
.map(a => a.bar ?
((a.bar = a.bar.reverse().flatMap((e,i) =>
a.bar.findIndex((f,j) => j > i && e.indent !== f.indent) !== -1 ?
(a.bar.find((f,j) => j > i && e.indent !== f.indent).indent > e.indent ? e : (a.bar.find((f,j) => j > i && e.indent !== f.indent).bar = a.bar.splice(i,a.bar.findIndex((f,j) => j > i && e.indent !== f.indent))))
: e
).reverse()), a)
: a
)
console.log(barArray);