Ninja Boy
Ninja Boy

Reputation: 1170

js - convert a whole array of strings into a string in which strings are separated by |

I have an array full of strings like [a,b,c,d]. I want to know the efficient way of converting this into 'a|b|c|d' using Javascript.

Thanks.

Upvotes: 0

Views: 93

Answers (3)

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

Try using array's join() method.The join() method joins array elements into a string.

    var arr = ['a','b','c','d'];//your aray
    var string =arr.join("|");
    console.log(string);

For more see here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222552

You can use array.join,

var pipe_delimited_= string_array.join("|");

DEMO

var string_array = ['a','b','c','d'];
var pipe_delimited = string_array.join("|");
console.log(pipe_delimited);

Upvotes: 1

charlietfl
charlietfl

Reputation: 171679

Pretty simple using Array.prototype.join()

    var data = ['a','b','c','d'];
    console.log(data.join('|'));

Upvotes: 2

Related Questions