簡體   English   中英

如何檢查字符串是否包含來自 NodeJS 中子字符串數組的文本,然后在數組中找到該 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ 的索引?

[英]How to check if a string contains text from an array of substrings in NodeJS and then find the index of that substring in the array?

我能夠確定問題的第一部分,即如果字符串包含來自 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

findIndex()方法返回數組中滿足提供的測試 function 的第一個元素的索引。 否則,它返回 -1,表示沒有元素通過測試。

var lifeSaver = allowedRoles.findIndex((substring) =>    
    currentRole.includes(substring)
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM