Using the .join method to convert an array to a comma-free string

Possible duplicate:
array join () method without separator

I use .join() to convert my array to a string so that I can output it to a text box when the user selects numbers in a calculator, I'm not quite sure how I can remove commas that are also displayed in the list. Can anyone advise how this can be achieved, or if there is another approach that I should use?

Js

 $(document).ready(function() { var total = 0; var arr = []; //Testing $('#calculator').children('.num').on('click', function(e) { var clickedNumber = $(this).data('id'); arr.push(clickedNumber); console.log(arr.join()); e.preventDefault(); }); });​ 

JS Fiddle http://jsfiddle.net/CVr25/

+70
javascript jquery
Aug 26 '12 at 17:11
source share
4 answers

Just:

 arr.join("") 
+163
Aug 26 2018-12-12T00:
source share

You can specify an empty string as an argument to join; if no argument is specified, a comma is used.

  arr.join(''); 

http://jsfiddle.net/mowglisanu/CVr25/1/

+24
Aug 26 '12 at 17:15
source share

.join() method has a parameter for the delimiter string. If you want it to be empty instead of the default comma, use

 arr.join(""); 
+16
Aug 26 '12 at 17:22
source share

All you have to do is:

 arr.join(''); 

Fiddle

+12
Aug 26 '12 at 17:15
source share



All Articles