![](/img/trans.png)
[英]Regex: Get string between first occurrence of string A and first occurrence of string B starting from the end of string A
[英]Get the index of a string from a string array based on the first occurrence of a string being a part of it
我有一個數組和字符串
const arr = ['home', 'about', 'contact']
const value = 'desktop_about'
const priority = ?
優先級應該是 1,因為那是要部分或全部包含在自身中的第一個字符串的索引。
計算這個的最佳方法是什么,我考慮了不同的循環,但似乎都過於復雜
您可以使用Array.prototype.findIndex
。
const priority = arr.findIndex(v => value.includes(v));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.