简体   繁体   English

如何通过使用javascript或jquery将字符串添加到数组中的每个字符串中?

[英]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.

相关问题 如何将字符串添加到 JavaScript 数组中每个元素的开头和结尾? - How to add a string to the start and the end of each element in a JavaScript array? 如何在JavaScript中的字符串的每个新行中添加“&gt;”? - How to add '>' to every new line in a string in javascript? 使用javascript大写,每行用一个新行将其分解,然后在每个新行的开头添加字符串 - Using javascript to Capitalize, Break it up with a new line every, and Add String to the beginning of each new line 使用jQuery在字符串中的每个单词之前添加“ +” - Add a “+” before each word in a string using jQuery 如何在每个随机生成的字符串Javascript中添加公共字符串 - How to add a common string in every randomly generated string Javascript 使用正则表达式在Javascript字符串中的每个下划线之后获取每个“单词” - Getting each 'word' after every underscore in a string in Javascript using regex 如何使用字符串合并跨度标签并使用jQuery将其添加到数组中 - How to club span tags from a string and add in to an array using jquery Javascript - 反转数组的每个字符串 - Javascript - Reversing every string of an array 如何在JavaScript中将jQuery数组转换为字符串数组 - How to convert a jquery array to a string array in javascript 如何使用jQuery / JavaScript将字符串的一部分与数组值匹配 - How to match a part of a string with array values using jquery / javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM