簡體   English   中英

根據作為其中一部分的字符串的第一次出現從字符串數組中獲取字符串的索引

[英]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.

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