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
id
ParentId
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".
parentId
parentId="id-1"