简体   繁体   中英

Javascript - Create nested object from array based on edge conditions

Recursion kills me every time.

I have the following array:

fooArray: Foo[] = 
[
  { foo: "a", indent: 0 },
  { foo: "b", indent: 1 },
  { foo: "c", indent: 2 },
  { foo: "d", indent: 1 },
  { foo: "e", indent: 2 },
  { foo: "f", indent: 2 },
  { foo: "g", indent: 1 },
  { foo: "h", indent: 0 },
]

I would like to map this to a different type of object (TS), and nest the objects based on their indent values as such:

interface Bar {
  foo: string,
  bar?: Bar[]
}

barArray: Bar[] = 
[
  { 
    foo: "a", 
    bar: [
      { foo: "b",
        bar: [{ foo: "c" }],
      { foo: "d",
        bar: [
          { foo: "e" }, 
          { foo: "f"}
        ] 
      },
      {
        foo: "g"
      }
    ]
  },
  { foo: "h" }
]

I've been trying to mess with reducers but needing to determine the level of recursion based on neighboring array members is really wracking my brain. If anyone has any thought it would be much appreciated!

My solution here is entirely a work in progress, so please do not test or try it just yet. I have to step away for a while and wanted to leave this here to come back to later:

 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,jb) => 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((fj,b) => a.splice(0.Math.max(a.slice(1),findIndex(g => g.indent === 0)+1.1)))),map(e => e,map((f?ia) => 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,jb) => a.splice(0.Math.max(a,slice(1).findIndex(g => g.indent === 0)+1.1)))),map(e => e,reverse().map((f:ia) => /*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((_:ia) => 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((_.ia) => 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((_.ia) => 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((ei) => a.bar,findIndex((fj) => 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((fj) => 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM