I Need flaten my array : How could I flatten my array of object :

    

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    var mylist = [{THEMEID: 1, 
      TITLE: "myTheme1", 
      PARENTID: 0, 
      children:[
        {ITEMID: 1, NAME: "myItem1", THEMEID: 1, PARENTID: 1, TITLE: "myItem1"},
        {THEMEID: 3, TITLE: "mySubTheme1", PARENTID: 1, children: [
            {ITEMID: 3, NAME: "myItem3", THEMEID: 3, PARENTID: 2, TITLE: "myItem3"}]}
      ]
    },
    {THEMEID: 2, TITLE: "myTheme2", PARENTID: 0, children: [
      {ITEMID: 4, NAME: "myItem4", THEMEID: 2, PARENTID: 1, TITLE: "myItem4"}]
    },
    {THEMEID: 6, TITLE: "myTheme3", PARENTID: 0, children:[
        {THEMEID: 5, TITLE: "mySubTheme3", PARENTID: 1, children: [
            {THEMEID: 4, TITLE: "mySubSubTheme3", PARENTID: 2, children: [
                {ITEMID: 2, NAME: "myItem2", THEMEID: 4, PARENTID: 3, TITLE: "myItem2"}
            ]}
        ]}
      ]
    }]

<!-- end snippet -->

In a array where are indexes of children are to record in the children array of the parent array:
Example :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    0:{THEMEID: 1, TITLE: "myTheme1", PARENTID: 0, children[0:1(index children),1:2]},

    (index)1:{ITEMID: 1, NAME: "myItem1", THEMEID: 1, PARENTID: 1, TITLE: "myItem1"},

    2:{THEMEID: 3, TITLE: "mySubTheme1", PARENTID: 1, children[0:3]},

    3:{ITEMID: 3, NAME: "myItem3", THEMEID: 3, PARENTID: 2, TITLE: "myItem3"}]}

<!-- end snippet -->

