Skip to content
Advertisement

Return True If Contains in String in ES6

How do i return true if location.pathname is found in of these strings is found in ES6?

  const noActionMenuRoutes = [
    '/master/employees',
    '/employees/dashboard',
    '/employees/requests',
    '/projects/myprojects',
  ];

  const noActionMenus = () => {
    if (location.pathname.some(noActionMenuRoutes)) {
      return true;
    } else {
      return false;
    }
  };

Advertisement

Answer

You can use Javascript array includes or indexOf function.

if (noActionMenuRoutes.some(route => location.pathname.includes(route)))
if (noActionMenuRoutes.some(route => location.pathname.indexOf(route) !== -1)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement