Question:
I receive a file path string like so:Answer:
split
on /
and then build it from there – you can do this concisely with map
:This is very simple – just set the
id
property to be whatever value you’re currently iterating over, and then if there exists a value before it (i.e. if this is not the first/root item) then set its parent
value to the previous value; otherwise, it’s an empty string. You could remove componentsArr
and refer to the splitting n
times but that’s mildly inefficient in my opinion.If you have better answer, please add a comment about this, thank you!