使用 JavaScript 内爆数组?

发布于 2024-10-02 06:33:15 字数 36 浏览 3 评论 0原文

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

Can I implode an array in jQuery like in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

凉薄对峙 2024-10-09 06:33:15

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

arrayName.join(delimiter);

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

arrayName.join(delimiter);
最丧也最甜 2024-10-09 06:33:15

像这样:

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

Like This:

[1,2,3,4].join('; ')
夜清冷一曲。 2024-10-09 06:33:15

Array.join 就是您需要,但如果您愿意, phpjs.org 的友好人员已经创建了 implode 为您服务。

然后就是一些稍微偏离主题的咆哮。正如 @jon_darkstar alreadt 指出的那样,jQuery 是 JavaScript,反之亦然。您需要了解 JavaScript 才能理解如何使用 jQuery,但这肯定不会有什么坏处,一旦您开始欣赏可重用性或开始着眼于更大的前景,您绝对需要学习它。

Array.join is what you need, but if you like, the friendly people at phpjs.org have created implode for you.

Then some slightly off topic ranting. As @jon_darkstar alreadt pointed out, jQuery is JavaScript and not vice versa. You don't need to 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.

伤痕我心 2024-10-09 06:33:15

为了将来参考,如果您想模仿 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

For future reference, if you want to mimic the behaviour of PHP's implode() when no delimiter is 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:

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
独﹏钓一江月 2024-10-09 06:33:15

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

工作示例

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

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

Working example

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

吻安 2024-10-09 06:33:15

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

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);

We can create alternative of implode of in javascript:

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);
同尘 2024-10-09 06:33:15

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

我已使用 substring 删除最后一个“;”没有必要在最后添加。
但如果你愿意,你可以使用 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;

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

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

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 substring something like:

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 $.each do not end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文