I'm trying to map object with keys so that I can easily return them. I can either get a string or RegExp which is the problem, setting the key as a string is easy.
This is how I set key for object when route is string, but the problem is that I cannot set regex as a key, it needs to be either a number or a string.
private nodesByRoute: { [key: string]: NavbarItemNode } = {};
this.nodesByRoute[node.route] = node;
So I would imagine the solution would look something like this:
private nodesByRoute: { [key: string | RegExp]: NavbarItemNode } = {};
My implementation is that I get a route string, let's say /category/items and if it matches it will return the node like this:
return this.nodesByRoute[routerLink];
But I also need this to work with regex because I can get something like this: /items/60
.
So I get items/
and the number of the item, thats why I need regex as a key.
Also I think I will also need logic when trying match the keys with my route. The route will always be a string but the key might be regex so I don't think this will work:
return this.nodesByRoute[routerLink];