I have an array like this:
["one", "foo", "two", "baz", "two", "one", "three", "lulz", "wtf", "three"]
1 2 2 1 3 3
Each string is a key to access an object literal. What I want to do is prepend the full path to children keys delimited by main keys in this case one, two, and three. This is the expected result:
[
'one', 'one.foo', 'one.two', 'one.two.baz', 'one.two', 'one',
'three', 'three.lulz', 'three.wtf', 'three'
]
I’ve been trying for past few hours with loops and extracting the duplicated items first and then trying to slice the array by a given start and end points. Then I tried with regex, just to see if it was possible but JS doesn’t handle recursion in regular expressions, and it got very complicated and probably unnecesary. I’m at that point where I got frustrated and nothing is working. I’ve no clue where to go now. I’m stuck.
Edit: I’ve been trying the above for a while because it seemed simpler but ideally all I want to extract is the chain of keys without the closing “tags”.
[
'one', 'one.foo', 'one.two', 'one.two.baz',
'three', 'three.lulz', 'three.wtf'
]

Can somebody enlighten me here?
Without recursion you can solve it like this; it keeps a common prefix and at every iteration it scans both left and right to find an identical item.
If there’s an identical item to the left, reduce the prefix (before emit); if there’s an item to the right, increase the prefix (after emit).
Demo