2

I am able to determine the first part of the question which is to return true if a string contains text from an array of substrings in NodeJS using this:

var allowedRoles = [
    "Area Director",
    "Managing Director",
    "Group Director"];

var currentRole = "USA Managing Director";
var lifeSaver = allowedRoles.some((substring) =>
    currentRole.includes(substring)
);
console.log(lifeSaver);

Here my result is true. But, I want to know that where in the allowedRole array my result returned true. (Expected answer: 1);

1
  • 1
    You can replace .some() with .findIndex() Commented Apr 17, 2020 at 10:25

1 Answer 1

2

Instead of using .some you can use .findIndex

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

var lifeSaver = allowedRoles.findIndex((substring) =>    
    currentRole.includes(substring)
);
Sign up to request clarification or add additional context in comments.

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.