0

I am Trying to create nested group of my javascript raw object.

item=[
{name:'Root1',id:1,parentId:null},
{name:'Root2',id:2,parentId:null},
{name:'Root3',id:4,parentId:null},
{name:'Root1.1',id:4,parentId:1},
{name:'Root1.1.1',id:5,parentId:4},
{name:'Root4',id:6,parentId:null}
];

this is my raw data,

I want this data in format some like bellow

var modified=
[
    {
        title:Root1,
        id:1,
        chields:
        {
            title:'Root 1.1',
            id:4,
            clields:
            {
                title:'Root 1.1.1',
                id:5,
                chield:
                {
                    title:'New',
                    id:null
                }
            }
        }
    }
];

1 Answers1

1

I got My Solution here

 var children = _.filter( array, function(child){ return child.parentid == parent.id; });

    if( !_.isEmpty( children )  ){
        if( parent.id == 0 ){
           tree = children;   
        }else{
           parent['children'] = children
        }
        _.each( children, function( child ){ unflatten( array, child ) } );                    
    }

    return tree;
}