Return True If Contains in String in ES6

Tags: ,



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;
    }
  };

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)


Source: stackoverflow