我有一个数组
let items = ['hat', 'shirt', 'glasses', 'jeans']this.mainItem是一个具有sample 'black-hat‘值的属性
如何检查this.mainItem是否包含数组中的一个字符串,以及是否存在返回该数组项的mtach,否则返回'‘空字符串?
发布于 2020-11-03 18:25:05
您可以使用find方法:
items.find((item) => this.mainItem.includes(item)) || ""发布于 2020-11-03 18:28:43
您可以使用.filter()函数:
items.filter((item) => this.mainItem.includes(item))// will return Array [ "hat" ]发布于 2020-11-03 18:30:51
您可以使用布尔值:
items.findIndex((el) => this.mainItem.includes(el)) !== -1我们使用新的findIndex方法并检查main tiem是否包含该元素。如果不是,则函数返回-1
https://stackoverflow.com/questions/64660899
复制相似问题