How does js get parameters from a path
Editor to share with you js how to get parameters from the path, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Get parameters from the path const getPathParams = (path, pathMap, serializer) = > {path = path.split ("/"); pathMap = pathMap.split ("/"); return pathMap.reduce ((acc, crr, I) = > {if (crr [0] = = ":") {const param = crr.substr (1); acc [param] = serializer & serializer [param]? Serializer [param] (path [I]): path [I];} return acc;}, {});}; getPathParams ("/ app/products/123", "/ app/:page/:id") / / {page: 'products', id:' 123'} getPathParams ("/ items/2/id/8583212", "/ items/:category/id/:id", {category: v = > ['Car',' Mobile', 'Home'] [v], id: v = > + v}); / / {category:' Home', id: 8583212} above are all the contents of the article "how to get parameters from a path by js". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!