0

I'm working with an array like this one :

var table = ['view-only-access', 'restricted-access', 'full-access'];

I wanted to find the index by only string like 'view' , 'restricted', or 'full'. I have tried the .indexOf() but it requires the full string. does anyone know how to do this ?

asked Oct 20, 2022 at 17:14

3 Answers 3

3

This should work table.findIndex(element=>element.includes('restricted'))

answered Oct 20, 2022 at 17:22
Sign up to request clarification or add additional context in comments.

Comments

1

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

answered Oct 20, 2022 at 17:19

2 Comments

startsWith might be a little better than includes.
@Andy, you are right, it's better ; thanks :)
0

var table = ['view-only-access', 'restricted-access', 'full-access'];
console.log(table.findIndex(i=>i.includes('view')));

answered Oct 20, 2022 at 17:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.