在javascript中查找单词的数组方法



我被困在工作中,需要帮助。我想在路径中找到一个单词并将其用作对象。

我的意思是,有这样的数组;

routes : [
0: {
...
path : 'x/x/create'
... }
1: {
...
path : 'y/y/y'
... }
]

和我想使用对象与路径:'x/x/create'。当然,它并不总是在第一位。*编辑:x总是在变化,所以我需要找到'创建'字

routes.find(i => i.path.includes('create')).word;

尝试使用Array.find

const routes = [
{ path: 'x/x/create' },
{ path: 'y/y/y' },
]
console.log(routes.find((route) => route.path === 'x/x/create'));
console.log(routes.find((route) => route.path === '/create'));

const routes = [
{
id: 1,
path: "x/x/create"
},
{
id: 2,
path: "y/y/y"
}
]
const result = routes.find(i => {
if (i.path.split('/')[2] === 'create') return i.id;
});
console.log(result); //{id:1, path:"x/x/create"}