0

I have an array, I want to filter out all the paths which are in form of /__/:__Id. I am currently using .endsWith property but I want to make a generic code which recognizes the pattern and doesn't return paths in pattern of /routename**/:nameId**

const ROUTES = [ { path: "/" }, { path: "/faqs" }, { path: "/contactus" }, { path: "/pricing" }, { path: "/products" }, { path: "/careers/:jobId" }, { path: "/careers" }, { path: "/about-us" }, { path: "/features" }, { path: "/usecase/:usecaseId" } ];

const selectedRoutes = ROUTES.map( (item) => {
  if (item.path === "/")
    return "";
  else
    return item.path;
}).filter( (item) => { return !item.endsWith("Id")}); 

console.log(selectedRoutes)

1
  • 1
    Could you try to clarify the question and also the code a little? You say I want to filter out all the paths which are in form of /__/:__Id and then doesn't return paths in pattern of /routename**/:nameId** - these two statements seem to contradict each other - or I am misunderstand you. In any case; clarification would help :) Commented Dec 13, 2022 at 9:30

3 Answers 3

1

You can achieve this requirement with the help of below RegExp

^\/(\\w)+\/:.*Id\/?$

RegEx explanation :

^\/ - Match the string start with forward slash
(\\w)+ - Matches the word characters (a-z, 0-9 and underscore), + is used to match the word one or more time.
\/? - Matches zero or one forward slash
$ - Denotes end of a string

Live Demo :

const ROUTES = [ { path: "/" }, { path: "/faqs" }, { path: "/contactus" }, { path: "/pricing" }, { path: "/products" }, { path: "/careers/:jobId" }, { path: "/careers" }, { path: "/about-us" }, { path: "/features" }, { path: "/usecase/:usecaseId" } ];

const re = new RegExp('^\/(\\w)+\/:.*Id\/?$', 'i');

const selectedRoutes = ROUTES.filter((item) => {
  return !item.path.match(re)
});

console.log(selectedRoutes);

Sign up to request clarification or add additional context in comments.

Comments

0

How about using regular expressions (regexp)?

const ROUTES = [ { path: "/" }, { path: "/faqs" }, { path: "/contactus" }, { path: "/pricing" }, { path: "/products" }, { path: "/careers/:jobId" }, { path: "/careers" }, { path: "/about-us" }, { path: "/features" }, { path: "/usecase/:usecaseId" } ];

const re = new RegExp('/.*/:.*Id', 'i');

const selectedRoutes = ROUTES.map((item) => {
  if (item.path === "/")
    return "";
  else
    return item.path;
}).filter((item) => {
  return !item.match(re)
});

console.log(selectedRoutes);

Comments

0

const ROUTES = [ { path: "/" }, { path: "/faqs" }, { path: "/contactus" }, { path: "/pricing" }, { path: "/products" }, { path: "/careers/:jobId" }, { path: "/careers" }, { path: "/about-us" }, { path: "/features" }, { path: "/usecase/:usecaseId" } ];

const selectedRoutes = ROUTES.map( (item) => {
  if (item.path === "/")
    return "";
else
return item.path;
}).filter( (item) => { return !item.includes(":")}); 

console.log(selectedRoutes);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.