Get the level of a hierarchy

前端 未结 4 1923
心在旅途
心在旅途 2020-12-29 15:37

I have an array of objects, Where each object has an id and a ParentId property (so they can be arranged in trees). They are in no particular order

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-29 15:59

    Here is your working code. Level starts at 2.

    ALERT: If a level cannot be calculated, the application may go into an infinite loop. So, make sure the parentId is valid for all objects and at least one of them have parentId="id-1".

    
    

提交回复
热议问题