Javascript Jquery - 从分隔字符串创建一个数组,然后评估其值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7387787/
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
Jquery - Create an array from a delimited string and then evaluate its values
提问by matteo
i've this problem i'm not really able to solve.
我有这个问题,我真的无法解决。
i have this string "B01, B20, B03, "
我有这个字符串“B01,B20,B03,”
i would like to create a jQuery array off this string using the "," as delimiter then remove the last element ( that would be blank ) and then for each value of the array make an alert.
我想使用“,”作为分隔符从这个字符串创建一个 jQuery 数组,然后删除最后一个元素(这将是空白),然后为数组的每个值发出警报。
Something like...
就像是...
var theString = "B01, B20, B03, ";
var theArray = (theString, ',');
theArray = remove_last_element; (??)
$('theArray').each(function() {
alert(theArray[?].value);
});
Any hint ?
任何提示?
Thanks !
谢谢 !
回答by Jon Hinson
var theString = "B01, B20, B03, ";
$.each(theString.split(",").slice(0,-1), function(index, item) {
alert(item);
});
Let me know if you have any questions.
如果您有任何问题,请告诉我。
回答by Guffa
There is no such thing as a "jQuery array". You use a Javascript array.
没有“jQuery 数组”这样的东西。您使用 Javascript 数组。
You can't just turn a string into an array because it contains comma separated values. You have to use something to parse the string, which would be the split
method in this case:
您不能仅仅将字符串转换为数组,因为它包含逗号分隔值。你必须使用一些东西来解析字符串,这将是split
这种情况下的方法:
var theString = "B01, B20, B03, ";
var theArray = theString.split(", ");
This produces an array with four items, as there is a trailing separator, so you can check for that and remove it:
这将生成一个包含四个项目的数组,因为有一个尾随分隔符,因此您可以检查并删除它:
if (theArray.length > 0 && theArray[theArray.length - 1].length == 0) {
theArray.pop();
}
Then you can either use plain Javascript or a jQuery method to loop the array. The plain Javascript looks like this:
然后你可以使用普通的 Javascript 或 jQuery 方法来循环数组。普通的 Javascript 如下所示:
for (var i = 0; i < theArray.length; i++) {
alert(theArray[i]);
}
Using a jQuery method looks like this:
使用 jQuery 方法如下所示:
$.each(theArray, function(index, item) {
alert(item);
});
You can also skip the step of removing the item, and just check for empty items in the loop:
您也可以跳过删除项目的步骤,只需检查循环中的空项目:
var theString = "B01, B20, B03, ";
var theArray = theString.split(", ");
for (var i = 0; i < theArray.length; i++) {
if (theArray[i].length > 0) {
alert(theArray[i]);
}
}
回答by lonesomeday
You don't need jQuery for this. Use String.prototype.split
to split the string, then loop through it with for
.
为此,您不需要 jQuery。使用String.prototype.split
通过它来分割字符串,然后循环for
。
var theString = "B01, B20, B03, ",
bits = theString.split(', ').slice(0, -1); // split the string and remove the last one
for (var i = 0; i < bits.length; i++) {
alert(bits[i]);
}
回答by bjornd
var theString = "B01, B20, B03, ",
theArray = theString.split(', ');
theArray.pop();
$(theArray).each(function() {
alert(this);
});
回答by Adam Bailin
The only jquery I used below was to "trim" the value to see if it was empty. Otherwise, I used split and splice (plain ol' javascript).
我在下面使用的唯一 jquery 是“修剪”值以查看它是否为空。否则,我使用 split 和 splice (plain ol' javascript)。
var theString = "B01, B20, B03, ";
var theArray = theString.split(',');
for(i=0; i<theArray.length; i++) {
// check if any array element is empty, if so, take it out of the array
if ($.trim(theArray[i]) == "")
theArray.splice(i, 1); // remove element since it's blank
}
// now you can iterate over the "real" values of the array
$.each(theArray, function(i, value) {
alert(value);
});
回答by loganfsmyth
var str = "B01, B20, B03, ";
$.each(str.split(','), function(i, val) {
val = $.trim(val);
alert(val);
});
回答by Sahil Muthoo
cleanString = function(str) {
return $.grep(str.split(','), function(elem) {
return ($.trim(elem) !== '')
});
}
This function will return an array with all split by comma with all the blanks removed. Check out $.grep.
此函数将返回一个数组,所有数组均以逗号分隔,并删除了所有空格。查看$.grep。
Usage
用法
cleanString("B01, B20, B03, ");
Alerting the array should be simple enough. It's just a matter of iterating over the returned array.
提醒阵列应该足够简单。这只是迭代返回的数组的问题。
var arr = cleanString("B01, B20, B03, ");
$.each(arr, function(index, value) {
alert(value);
});