Conditional anchor in structure url
There was a typo hidden in there, otherwise the logic works. To get children as anchors in a structure, the following should do the trick:

{parent.uri}{% if object.getDescendants().total() != 0 %}/{% else %}#{% endif %}{slug}