Implode an array with JavaScript?
Asked Answered
A

7

321

Can I implode an array in jQuery like in PHP?

Armyworm answered 10/11, 2010 at 17:0 Comment(0)
W
607

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

arrayName.join(delimiter);
Wellman answered 10/11, 2010 at 17:3 Comment(1)
Can a moderator force accept this answer or some how make it go to the top? I missed this answer initially reading this.Brame
H
73

Like This:

[1,2,3,4].join('; ')
Housetop answered 10/11, 2010 at 17:2 Comment(0)
L
28

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.

Larena answered 10/11, 2010 at 17:10 Comment(2)
then again, javascript IS my favorite jQuery plugin ;-)Wellman
This comment aged wellFancyfree
T
21

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
Tarra answered 20/5, 2014 at 3:10 Comment(0)
R
12

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);
Rickard answered 27/2, 2019 at 6:28 Comment(0)
L
0

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);
Ladybird answered 26/9, 2012 at 18:20 Comment(1)
Textbook case of things not to do. Do not perform string concatenation in a loop. Do not replace fast built in functions with your own slow code. Using the Array constructor instead of literals is unecessarily verbose. If you are desperate to recreate the implode function the smart thing would be to wrap the built in join method with your own function. The smarter thing is to learn the language you are programming in.Housetop
W
0

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.

Widdershins answered 3/3, 2013 at 23:24 Comment(4)
If you were getting a ',' instead of a ';' you were forgetting to pass the delimiter in to the join method. Try your_array.join(';')Housetop
I tried it in that time but it didn't work in IE (I do not remember the version exact, but I think that was IE6). I needed to support it in that time (Enterprise app...). In newers browsers your note probably make sense. Thanks for the note Mike and thanks for downvote BEFORE asking WHY I have used this method.Widdershins
The join parameter works in IE 4, 5, 5.5, 6+. I't might have worked in IE3 but I can't get my copy to execute any JavaScript, so I can't test.Housetop
Yes, it works. But not with ";". It join but replaces ";" with ",", at least on Server 2003.Widdershins

© 2022 - 2024 — McMap. All rights reserved.