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);
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)
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.