Transform Javascript Array into delimited String

后端 未结 7 706
别那么骄傲
别那么骄傲 2020-12-03 04:23

I have a Javascript string array with values like A12, B50, C105 etc. and I want to turn it into a pipe delimited string like this: A12|B50|C105...

How could I do th

相关标签:
7条回答
  • 2020-12-03 04:58
    var pipe_delimited_string = string_array.join("|");
    

    Array.join is a native Array method in Javascript which turns an array into a string, joined by the specified separator (which could be an empty string, one character, or multiple characters).

    0 讨论(0)
  • 2020-12-03 05:00

    I using lodash@4.17.10. It's very good with array and object

    _.join(['a', 'b', 'c'], '~');
    // => 'a~b~c'
    

    Ref lodash

    0 讨论(0)
  • 2020-12-03 05:01

    For a native JavaScript array then myArray.join('|') will do just fine.

    On the other hand, if you are using jQuery and the return value is a jQuery wrapped array then you could do something like the following (untested):

    jQuerySelectedArray.get().join('|')
    

    See this article for more information.

    0 讨论(0)
  • 2020-12-03 05:15

    Use JavaScript 'join' method. Like this:

    Array1.join('|')

    Hope this helps.

    0 讨论(0)
  • 2020-12-03 05:22

    No need for jQuery. Use Javascripts join() method. Like

    var arr = ["A12", "C105", "B50"],
        str = arr.join('|');
    
    alert(str);
    
    0 讨论(0)
  • 2020-12-03 05:22

    var checked = $(':input[type="checkbox"]:checked').map(function(){return this.value}).get(); console.log(checked.join(", "));

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