I have an array of strings :
listOfTypes = ['blogPost', 'work']
And I have a routeName
routeName = blogPost-slug or routeName = blogPost;
When routeName contains = blogPost I want to return string "page".
If routeName start with string- (example: blogPost-), I want to return the type (from listOfTypes)
I made a forEach function that works. Only, I get several results (it's normal) when I only want one (if it is present in the array, then I return either the type or page)
listOfTypes = ['blogPost', 'work'];
// routeName is retrieved dynamically. This is for example, "blogPost" or "blogPost-slug" or "work-slug"
listOfTypes.forEach(type => {
// Check if route name starts with type + "-" because it's separator with subroute
// (/blogPost is a page, /blogPost-slug is a blogPost so check for blogPost-)
if(routeName.startsWith(type + '-')) {
return console.log(type);
} else {
return console.log('page');
}
});
// result: 'blogPost' and 'page'
// expected result: 'blogPost'
How can I do this and only get either the type or "page" if it doesn't match?
Thank you