Array conversion function tree

// array into tree 
         function    arraytotree (ARR) {
                 var Top = [], Sub = [], tempObj = {}; 

                arr.forEach ( function (Item) {
                     IF (item.parentId === 0) { // top Categories 
                        top.push (Item) 
                    } the else { 
                        sub.push (Item) // other Categories 
                    } 
                    item.children = []; // Moran adds the children property 
                    tempObj [item.id] Item = // with the current classification id do key, stored in the tempObj
                }) 

                Sub.forEach ( function (Item) {
                     // get the parent 
                    var parent = tempObj [item.parentId] {|| 'children' : []}
                     // the current classification is added to the children of the parent 
                    parent.children .push (Item) 
                }) 

                return Top 
            },

Guess you like

Origin www.cnblogs.com/yscec/p/12210832.html