我用的是这样的数组:
var table = ['view-only-access', 'restricted-access', 'full-access'];我只想通过像'view' , 'restricted', or 'full'.这样的字符串来查找索引,我尝试了.indexOf(),但是它需要完整的字符串。有人知道怎么做吗?
发布于 2022-10-20 17:22:22
这应该适用于table.findIndex(element=>element.includes('restricted'))
发布于 2022-10-20 17:16:39
var table = ['view-only-access', 'restricted-access', 'full-access'];
console.log(table.findIndex(i=>i.includes('view')));
发布于 2022-10-20 17:19:19
const
table = ['view-only-access', 'restricted-access', 'full-access']
, f_search = str => table.findIndex( x => x.startsWith( str ) )
;
console.log( f_search('full') ) // 2
console.log( f_search('restricted') ) // 1
console.log( f_search('view') ) // 0
https://stackoverflow.com/questions/74143836
复制相似问题