This patch revives the feature existed in the initial version of imenu.el, but that was unfortunately removed later. When comparing it with the version in breadcrumb-jump that was discussed in https://lists.gnu.org/archive/html/emacs-devel/2023-12/msg00278.html the advantage of this version is that it doesn't use 'cl-loop':