如何筛选以下数组
[ '/pageA/pageAA.html',
'/pageB.html',
'/pageC.html',
'/pageC/',
'/pageC/pageW/pageC.html',
'/' ]
得到
[ '/pageA/pageAA.html',
'/pageB.html',
'/pageC/pageW/pageC.html' ]
规则如下:
-
路径不以结尾
/
-
路径不应类似于以
/
前任:
'/pageC/'
和
'/pageC.html'
是相似的
我已经做了以下的工作,但它不干净,也许有一个更干净的方法来使用regex或类似的东西
const routes = ['/pageA/pageAA.html',
'/pageB.html',
'/pageC.html',
'/pageC/',
'/pageC/pageW/pageC.html',
'/'
]
const filterRoutes = routes => {
var unWontedRoutes = []
for (let i = 0; i < routes.length - 1; i++) {
for (let j = i + 1; j < routes.length; j++) {
if (Math.abs(routes[i].length - routes[j].length) == 4) {
var a = routes[i].length < routes[j].length ? routes[j] : routes[i]
var b = routes[i].length > routes[j].length ? routes[j] : routes[i]
if (a.replace(b.slice(0, b.length - 1), '') === '.html') {
unWontedRoutes.push(a)
}
}
}
}
return unWontedRoutes
}
const unWontedRoutes = filterRoutes(routes)
const newRoutes = routes.filter(route => (!(unWontedRoutes.indexOf(route) > -1) && !route.endsWith('/')))
console.log(newRoutes)