I'm currently working on developing a function that can handle a flat array of string decimal integers representing nodes in a tree. Each period signifies hierarchy within the tree. The goal is to build functions called prevNode
and nextNode
, which will take three parameters: ids, id, planeLock
. If a node does not have a previous or next id
, the function will return false
. When planeLock
is set to true
, the navigation will stay on the same level instead of moving to the child nodes (e.g., from 1
to 0.1
). This is referred to as navigating within the same plane, i.e., sibling nodes rather than their deepest children.
var ids = [
'0',
'0.1',
'1',
'2',
'2.0',
'2.1',
]
prevNode(ids, '0')
->false
// no previous nodeprevNode(ids, '1', true)
->0
// stays on the same plane with plane lockprevNode(ids, '1')
->0.1
// previous node in the treeprevNode(ids, '2.0', true)
->false
prevNode(ids, '2.0')
->2
// goes up one node
What would be the best approach to parse these strings for the intended outcomes?