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 ?

3 Answers 3

3

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

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

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')));

Comments

Your Answer

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.