如果在ES6中找到这些字符串中的location.pathname
,我如何返回true
?
const noActionMenuRoutes = [
'/master/employees',
'/employees/dashboard',
'/employees/requests',
'/projects/myprojects',
];
const noActionMenus = () => {
if (location.pathname.some(noActionMenuRoutes)) {
return true;
} else {
return false;
}
};
使用findIndex
return noActionMenuRoutes.findIndex(el => el.includes(location.pathname)) != -1;
您可以使用Javascript数组includes
或indexOf
函数。
if (noActionMenuRoutes.some(route => location.pathname.includes(route)))
if (noActionMenuRoutes.some(route => location.pathname.indexOf(route) !== -1)
您可以使用string.includes()
来确定是否可以在另一个字符串中找到一个字符串。
由于noActionMenuRoutes
是一个数组,因此对其进行迭代并根据每个字符串进行验证
const noActionMenus = () => {
return noActionMenuRoutes.some(x => location.pathname.includes(x))
};
const noActionMenuRoutes = [
'/master/employees',
'/employees/dashboard',
'/employees/requests',
'/projects/myprojects',
];
const noActionMenus = () => noActionMenuRoutes.some(path => location.pathname.includes(path));
console.log(noActionMenus() )