我能够确定问题的第一部分,如果字符串包含 NodeJS 中子字符串数组中的文本,则返回 true:
var allowedRoles = [
"Area Director",
"Managing Director",
"Group Director"];
var currentRole = "USA Managing Director";
var lifeSaver = allowedRoles.some((substring) =>
currentRole.includes(substring)
);
console.log(lifeSaver);
这里我的结果是正确的。 但是,我想知道在 allowedRole 数组中我的结果返回 true 的位置。 (预期答案:1);
最佳答案
您可以使用 .findIndex
而不是使用 .some
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)
);
关于javascript - 如何检查一个字符串是否包含 NodeJS 中子字符串数组中的文本,然后找到该子字符串在数组中的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61269282/