Get the level of a hierarchy

前端 未结 4 1908
心在旅途
心在旅途 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 16:03

    I'm not sure where you get the value for level so I'll assume that its just an integer. BTW, you can add the level property to each of your array's item by looping through it.

    for (var i = 0, l = data.length; i < l; i++) { 
        data[i].level = i
    }
    

    which will give

    data = [{id:"1", parentId:"3", level:0 }, {id:"2", parentId:"1", level:1 } ...]
    

提交回复
热议问题