用 jQuery/Javascript 内爆一个数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4146927/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:06:25  来源:igfitidea点击:

Implode an array with jQuery/Javascript?

javascriptjqueryimplode

提问by Omega

Can I implode an array in jQuery like in PHP?

我可以像在 PHP 中一样在 jQuery 中内爆数组吗?

回答by jon_darkstar

You can do this in plain JavaScript, use Array.prototype.join:

您可以在纯 JavaScript 中执行此操作,请使用Array.prototype.join

arrayName.join(delimiter)

I don't know of any jQuery function that is any better.

我不知道有任何更好的 jQuery 函数。

回答by mikerobi

Like This:

像这样:

[1,2,3,4].join('; ')

回答by nikc.org

Array.joinis what you need, but if you like, the friendly people at phpjs.orghave created implodefor you.

Array.join是您所需要的,但如果您愿意,phpjs.org 上的友好人员已implode为您创建。

Then some slightly off topic ranting. As @jon_darkstar alreadt pointed out, jQuery is JavaScript and not vice versa. You don't needto know JavaScript to be able to understand how to use jQuery, but it certainly doesn't hurt and once you begin to appreciate reusability or start looking at the bigger picture you absolutely need to learn it.

然后是一些稍微偏离主题的咆哮。正如@jon_darkstar 已经指出的那样,jQuery 是 JavaScript,反之亦然。您不需要了解 JavaScript 就能够理解如何使用 jQuery,但它当然不会受到伤害,一旦您开始欣赏可重用性或开始查看更大的图景,您绝对需要学习它。

回答by Robbie Averill

For future reference, if you want to mimic the behaviour of PHP's implode()when no delimiteris specified(literally just join the pieces together), you need to pass an empty string into Javascript's join()otherwise it defaults to using commas as delimiters:

为了将来参考,如果您想没有指定分隔符的情况下模仿PHP 的implode()行为(实际上只是将各个部分连接在一起),您需要将一个空字符串传递给 Javascript,join()否则它默认使用逗号作为分隔符:

var bits = ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'];
alert(bits.join());    // H,e,l,l,o, ,W,o,r,l,d
alert(bits.join(''));  // Hello World

回答by Deepu Reghunath

Use join()method creates and returns a new string by concatenating all of the elements in an array.

使用join()方法通过连接数组中的所有元素来创建并返回一个新字符串。

Working example

工作示例

var arr= ['A','b','C','d',1,'2',3,'4'];
var res= arr.join('; ')
console.log(res);

回答by Vijay Verma

We can create alternative of implode of in javascript:

我们可以在 javascript 中创建 implode 的替代方案:

function my_implode_js(separator,array){
       var temp = '';
       for(var i=0;i<array.length;i++){
           temp +=  array[i] 
           if(i!=array.length-1){
                temp += separator  ; 
           }
       }//end of the for loop

       return temp;
}//end of the function

var array = new Array("One", "Two", "Three");


var str = my_implode_js('-',array);
alert(str);

回答by jaysponsored

array.joinwas not recognizing ";" how a separator, but replacing it with comma. Using jQuery, you can use $.eachto implode an array (Note that output_saved_json is the array and tmp is the string that will store the imploded array):

array.join不认识“;” 如何分隔符,但用逗号替换它。使用 jQuery,您可以使用$.each内爆数组(注意 output_saved_json 是数组,tmp 是将存储内爆数组的字符串):

var tmp = "";
$.each(output_saved_json, function(index,value) {
    tmp = tmp + output_saved_json[index] + ";";
});

output_saved_json = tmp.substring(0,tmp.length - 1); // remove last ";" added

I have used substring to remove last ";" added at the final without necessity. But if you prefer, you can use instead substringsomething like:

我已经使用子字符串删除最后一个“;” 在没有必要的情况下添加到最后。但是如果你愿意,你可以使用substring类似的东西:

var tmp = "";
$.each(output_saved_json, function(index,value) {
    tmp = tmp + output_saved_json[index];

    if((index + 1) != output_saved_json.length) {
         tmp = tmp + ";";
    }
});

output_saved_json = tmp;

I think this last solution is more slower than the 1st one because it needs to check if index is different than the lenght of array every time while $.eachdo not end.

我认为最后一个解决方案比第一个解决方案更慢,因为它每次都需要检查索引是否与数组的长度不同,而$.each不要结束。