Javascript 用逗号 (,) 分隔并检查是否有任何值 = "something"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12747943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Separate by comma (,) and check if any of the values is = "something"
提问by Mark Topper
How can I somehow split/separate my JavaScript variable by comma (,).
And then check if value-of-any-of-the-separated-strings = "something"
我怎样才能用逗号 (,) 以某种方式拆分/分隔我的 JavaScript 变量。
然后检查是否value-of-any-of-the-separated-strings = "something"
For example, my variable has the value 1,2,3,4,5,6,7,8,9,10,2212312
, and I want to check if any of the numbers are = 7
in a IF-Statement.
例如,我的变量的值为1,2,3,4,5,6,7,8,9,10,2212312
,我想检查是否有任何数字= 7
在 IF 语句中。
Does anyone have any ideas how this can be done?
有没有人有任何想法如何做到这一点?
回答by Ian
First, split the string by ","
. Then, use indexOf
on the split-string array to see if the target string is found (-1
means it wasn't found in the array). For example:
首先,将字符串拆分为","
。然后,indexOf
在拆分字符串数组上使用以查看是否找到了目标字符串(-1
意味着在数组中未找到它)。例如:
var str = "1,2,3,4,5,6,7,8,9,10,10,2212312";
var split_str = str.split(",");
if (split_str.indexOf("7") !== -1) {
// Original string contains 7
}
References:
参考:
String.prototype.split
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/splitArray.prototype.indexOf
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
String.prototype.split
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/splitArray.prototype.indexOf
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
回答by zzzzBov
This is a simple application of Array.prototype.some
:
这是一个简单的应用程序Array.prototype.some
:
var yourVar = '1,2,3,4,5,6,7,8,9,10,2212312';
function isSeven(val) {
return val === '7';
}
if (yourVar.split(',').some(isSeven)) {
//do stuff
}
Another common waythis could be written is:
另一种常见的写法是:
if (~yourVar.split(',').indexOf('7')) {
//do stuff
}
Or if Array.prototype.contains
has been defined:
或者如果Array.prototype.contains
已经定义:
if (yourVar.split(',').contains('7')) {
//do stuff
}
Or if you want to use a regular expression:
或者,如果您想使用正则表达式:
if (/(?:^|,)7(?:,|$)/.test(yourVar)) {
//do stuff
}
Note:Array.prototype.some
, Array.prototype.indexOf
and Array.prototype.contains
all require polyfills to work correctly cross browser.
注意:Array.prototype.some
,Array.prototype.indexOf
并且Array.prototype.contains
都需要 polyfills 才能跨浏览器正常工作。
回答by Bartek
Split it into an Array, then use indexOf to see if it's there. If it returns -1, it isn't.
将其拆分为一个数组,然后使用 indexOf 来查看它是否存在。如果它返回-1,则不是。
"1,2,3,4,5,6,7,8,9,10,2212312".split(",").indexOf("7")
回答by epascarello
Use splitand Array.indexOf()
var str = "1,2,3,4,5,6,7,8,9,10,2212312";
var num = 7;
var pieces = str.split(",");
var index = pieces.indexOf(num.toString());
It can be done with regular expressions too
也可以用正则表达式来完成
var str = "1,2,3,4,5,6,7,8,9,10,2212312";
var num = 7;
var re = new RegExp("(^|,)" + num + "($|,)");
alert(re.test(str));
回答by jbabey
var someString = '1,2,3,4,5,6,7,8,9,10,2212312';
var splitArray = someString.split(',');
var sevenPosition = splitArray.indexOf('7');
回答by KooiInc
You could use Array.filter
, something like:
您可以使用Array.filter
,例如:
var values = '1,2,3,4,5,6,7,8,9,10,2212312'.split(','), find = 7;
if ( values.filter(function(a){return +a === find;}).length ) { /* ... */ }
回答by Vishal Zanzrukia
man i hope it will help you.
伙计,我希望它会帮助你。
var yourValues = '1,2,3,4,5,6,7,8,9,10,2212312';
var array = yourValues.split(",");
boolean isValue = false;
for(i in array)
{
if(array[i]=='7')
{
isValue=true;
}
}
if(isValue)
alert("your number is in the string");
else
alert("your number is in the string");
回答by Carlos Martinez T
Are you looking for the "contains" function. You can use jQuery for this.
您是否正在寻找“包含”功能。您可以为此使用 jQuery。
if ($.inArray(7, value-of-any-of-the-seperated-strings))
{
console.log("there is a 7!")
}