1
const item = {
  id: 'item1',
  children: [ 
    { id: 'item1-1',
      children: [
        { id: 'item1-1-1' },
        { id: 'item1-1-2' },
        { id: 'item1-1-3' },
      ]
    },
    { id: 'item1-2',
      children: [
        { id: 'item1-2-1' }
      ]
    }
  ]
}

Like this,

function getLevelOfId(){
  ...
}

getLevelOfId('item1') =====> return 1
getLevelOfId('item1-2') =====> return 2
getLevelOfId('item1-1-1') =====> return 3
getLevelOfId('item1-1-2') =====> return 3

How to get specific object's depth with JavaScript?

Not use of id string. like ('item1-2').split('-').length Because each object has randomic id. Is there a simple way?

ton1
  • 7,238
  • 18
  • 71
  • 126
  • Hi! Please take the [tour] (you get a badge!) and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Your best bet here is to do your research, [search](/help/searching) for related topics on SO, and give it a go. (You'll probably want to use recursion.) ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. – T.J. Crowder Mar 08 '19 at 08:19
  • Possible duplicate of [How to check the depth of an object?](https://stackoverflow.com/questions/13523951/how-to-check-the-depth-of-an-object) – Souritra Das Gupta Mar 08 '19 at 08:22
  • @SouritraDasGupta It's different. That question is about `maximum depth` and this is about `specific depth`. – ton1 Mar 08 '19 at 09:31

2 Answers2

3

You need to iterate all objects and if found, take one for each level for the recursion depth.

function getLevelOfId(object, id) {
    var level;
    if (object.id === id) return 1;
    object.children && object.children.some(o => level = getLevelOfId(o, id));
    return level && level + 1;
}

const item = { id: 'item1', children: [{ id: 'item1-1', children: [{ id: 'item1-1-1' }, { id: 'item1-1-2' }, { id: 'item1-1-3' }] }, { id: 'item1-2', children: [{ id: 'item1-2-1' }] }] };

console.log(getLevelOfId(item, 'item1'));     // 1
console.log(getLevelOfId(item, 'item1-2'));   // 2
console.log(getLevelOfId(item, 'item1-1-1')); // 3
console.log(getLevelOfId(item, 'item1-1-2')); // 3
console.log(getLevelOfId(item, 'foo'));       // undefined
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

if the structure id & children is fixed, how about search the whole value like "item1-1-1" in the json string:

{"id":"item1","children":[{"id":"item1-1","children":[{"id":"item1-1-1"},{"id":"item1-1-2"},{"id":"item1-1-3"}]},{"id":"item1-2","children":[{"id":"item1-2-1"}]}]}

level = (number of "{") - (number of "}") // before the searched positon of the string

nieben
  • 49
  • 5
  • The case of item `item1-1-1` and item `item1-1-2`, these two has same level. Perhaps the result of both will be different. – ton1 Mar 08 '19 at 09:29
  • if the structure is fixed, the results are the same, as indicated, the number of "}" is considered too, so item1-1-1 = 3 - 0, item1-1-2 = 4 - 1 – nieben Mar 08 '19 at 10:04
  • Oh, I didn't know that. That makes sense! I will try this way too! – ton1 Mar 08 '19 at 10:22