0

I'm trying to filter an array like tree structure recursively to it will be limited to maximum number of items to be display.


export interface MyType {
  children: MyType[],
  id: number;
name:string;
}

items: MyType[] = [{
  children: [{ 
     children:[]
     id: 3,
     name: 'three'
   },
   { 
     children:[]
     id: 4,
     name: 'four'
   }
  ],
  id: 1,
  name: 'One'
},
{
  children: [{ 
    children: [],
    id: 5,
    name: 'five'
  }
  ],
  id: 2,
  name: 'two'
}
];

I would like to create a function to filter the above array so that its can only have maximum 4 items of MyType object.

So in this case new Array would be:

[{
  children: [{ 
     children:[]
     id: 3,
     name: 'three'
   },
   { 
     children:[]
     id: 4,
     name: 'four'
   }
  ],
  id: 1,
  name: 'One'
},
{
  children: [],
  id: 2,
  name: 'two'
}
];

Hung Bui
  • 313
  • 4
  • 17

0 Answers0