Remove array delimiters

前端 未结 5 1405
终归单人心
终归单人心 2021-01-18 23:51

Is it possible remove delimiters when you array.toString? (javascript)

var myArray = [ \'zero\', \'one\', \'two\', \'three\', \'four\', \'five\' ];
var resul         


        
相关标签:
5条回答
  • 2021-01-18 23:58

    Normally the separator is , to remove the separetor use the function .replace(/,/g,'')

    But if there is comma , in the data you might want to consider something like

    var str = '';
    for(var x=0; x<array.length; x++){
      str += array[x];
    }
    
    0 讨论(0)
  • 2021-01-19 00:02

    You can you the replace[MDN] method:

    var arr = [1,2,3,4],
        arrStr = arr.toString().replace(/,/g, '');
    
    0 讨论(0)
  • 2021-01-19 00:09

    You could use join instead of toString -

    var myArray = [ 'zero', 'one', 'two', 'three', 'four', 'five' ];
    var result = myArray.join('');
    

    join will join all the elements of the array into a string using the argument passed to the join function as a delimiter. So calling join and passing in an empty string should do exactly what you require.

    Demo - http://jsfiddle.net/jAEVY/

    0 讨论(0)
  • 2021-01-19 00:09

    You can use array.join("separator") instead.

    ["a", "b", "c"].join("");  // "abc"
    ["a", "b", "c"].join(" "); // "a b c"
    ["a", "b", "c"].join(","); // "a,b,c"
    
    0 讨论(0)
  • 2021-01-19 00:21

    You can just use Array.join() to achieve this:

    var arr = ["foo", "bar", "baz"],
        str1 = arr.join(""), // "foobarbaz"
        str2 = arr.join(":"); // "foo:bar:baz"
    

    Whatever you specify in the join method will be used as the delimiter.

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