本文介绍了Javascript-将值从数组匹配到变量的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将数组中的值匹配到可变字符串.因此,例如,如果我有一个数组和一个变量:
I'm trying to match values from an array to a variable string.So if, for example, I have an array and a variable :
var array = ['Potato', 'Cheese', 'Bikini', 'Truck', 'Express'];
var something = 'Potato Thief';
我希望函数返回"True",因为变量在数组中包含"Potato".
I would like my function to return "True" since the variable includes 'Potato' in the array.
我目前有这样的东西...
I currently have something like this at the moment...
if(array.indexOf(something)!=-1)
{
true;
}
else
{
false;
}
感谢您的帮助!
推荐答案
您不需要if语句
return array.indexOf(something)!=-1
这篇关于Javascript-将值从数组匹配到变量的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!