Convert javascript array to string

前端 未结 14 835
逝去的感伤
逝去的感伤 2020-11-29 16:41

I\'m trying to iterate over a \"value\" list and convert it into a string. Here is the code:

var blkstr = $.each(value, function(idx2,val2) {                        


        
相关标签:
14条回答
  • 2020-11-29 17:38

    Use join() and the separator.

    Working example

    var arr = ['a', 'b', 'c', 1, 2, '3'];
    
    // using toString method
    var rslt = arr.toString(); 
    console.log(rslt);
    
    // using join method. With a separator '-'
    rslt = arr.join('-');
    console.log(rslt);
    
    // using join method. without a separator 
    rslt = arr.join('');
    console.log(rslt);

    0 讨论(0)
  • 2020-11-29 17:42

    You can use .toString() to join an array with a comma.

    var array = ['a', 'b', 'c'];
    array.toString(); // result: a,b,c
    

    Or, set the separator with array.join('; '); // result: a; b; c.

    0 讨论(0)
提交回复
热议问题