Reputation: 1170
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
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
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
Reputation: 171679
Pretty simple using Array.prototype.join()
var data = ['a','b','c','d'];
console.log(data.join('|'));
Upvotes: 2