[英]How to add a string into each and every string in an array by using javascript or jquery?
I want to add "*." 我要添加“ *”。 at opening and ";" 在开头和“;” at ending on each array element. 在每个数组元素的结尾处。 Below is the sample code. 下面是示例代码。
var ext = getExt(filename);
var s = '<%=AllowedExtensions %>';
var match = s.split(', ');
In the above code in 'match' I use to get ["jpg", "png", "txt"] like this. 在上面的“ match”代码中,我使用了这样的[[jpg],“ png”,“ txt”]。 So here on each array element I want to prefix with " ." 所以在这里,我想在每个数组元素前加上“ ”作为前缀。 and at end of each want to add ";" 并在每个结尾处添加“;” and also it should give me a string such as " .jpg; .png; .txt;" 并且还应该给我一个字符串,例如“ .jpg; .png; .txt;”。 . 。
ES5 code: ES5代码:
match = match.map(function( word ) {
return '*.' + word + ';';
}).join('');
ES3 code: ES3代码:
for(var i = 0, len = match.length; i < len; i++) {
match[ i ] = '*.' + match[ i ] + ';';
}
match = match.join('');
Demo: http://jsfiddle.net/PNDr6/1/ 演示: http : //jsfiddle.net/PNDr6/1/
尝试这个:
var matchString = '.' + match.join(';.') + ';'
You can do something like this: 您可以执行以下操作:
for(var k = 0; k < match.length; k++)
{
match[k] = '*.' + match[k];
}
var allExten = match.join(';');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.