true如果location.pathname在 ES6 中找到这些字符串,我如何返回? const noActionMenuRoutes = [ '/master/employees', '/employees/dashboard', '/employees/requests', '/projects/myprojects', ]; const noActionMenus = () => { if (location.pathname.some(noActionMenuRoutes)) { return true; } else { return false; } };
4 回答
慕运维8079593
TA贡献1876条经验 获得超5个赞
使用查找索引
return noActionMenuRoutes.findIndex(el => el.includes(location.pathname)) != -1;
繁星coding
TA贡献1797条经验 获得超4个赞
您可以使用 Javascript 数组includes
或indexOf
函数。
if (noActionMenuRoutes.some(route => location.pathname.includes(route)))
if (noActionMenuRoutes.some(route => location.pathname.indexOf(route) !== -1)
狐的传说
TA贡献1804条经验 获得超3个赞
您可以用来string.includes()
确定一个字符串是否可以在另一个字符串中找到。
正如noActionMenuRoutes
数组一样,对其进行迭代并针对每个字符串进行验证
const noActionMenus = () => { return noActionMenuRoutes.some(x => location.pathname.includes(x)) };
智慧大石
TA贡献1946条经验 获得超3个赞
const noActionMenuRoutes = [
'/master/employees',
'/employees/dashboard',
'/employees/requests',
'/projects/myprojects',
];
const noActionMenus = () => noActionMenuRoutes.some(path => location.pathname.includes(path));
console.log(noActionMenus() )
添加回答
举报
0/150
提交
取消