首页 > 解决方案 > 如果值包含,如何获取数组中键的索引?

问题描述

这就是我的意思。

var brands = [['brand: LV'], ['brand: Apple']]...

有了这个,我想定位(索引)say..Apple。手动,我可以轻松做到

brands[1]

但这会动态更新,因此索引将是随机的。

标签: javascriptarraysindexof

解决方案


使用findIndex()indexOf()

示例:brands.findIndex(doc => doc[0] === 'brand: Apple')


推荐阅读