在字符串javascript中查找数组的第一个索引

在字符串javascript中查找数组的第一个索引,javascript,ecmascript-6,Javascript,Ecmascript 6,有一个字符串变量的路径类似于/app/something/xx/4/profile 此外,还有一个类似字符串的数组** const arr=[ {name:'xx',etc...}, {name:'yy',etc...}, {name:'zz',etc...} ] 我想以最简单的方式找到字符串变量具有名称的数组的第一个索引。使用哪个: 返回数组中满足提供的测试函数的第一个元素的索引。否则,它返回-1,表示没有元素通过测试 const str=“/app

有一个字符串变量的路径类似于/app/something/xx/4/profile 此外,还有一个类似字符串的数组**

const arr=[
     {name:'xx',etc...}, 
     {name:'yy',etc...},
     {name:'zz',etc...}
    ]
我想以最简单的方式找到字符串变量具有名称的数组的第一个索引。

使用哪个:

返回数组中满足提供的测试函数的第一个元素的索引。否则,它返回-1,表示没有元素通过测试

const str=“/app/something/xx/4/profile”;
const arr=[{name:“xx”},{name:“yy”},{name:“zz”}];
const index=arr.findIndex(e=>str.includes(e.name));
log({index})使用以下选项:

返回数组中满足提供的测试函数的第一个元素的索引。否则,它返回-1,表示没有元素通过测试

const str=“/app/something/xx/4/profile”;
const arr=[{name:“xx”},{name:“yy”},{name:“zz”}];
const index=arr.findIndex(e=>str.includes(e.name));
log({index})