0

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.

4
  • 1
    Use the .join() method... ['a','b','c','d'].join('|') -> "a|b|c|d" Commented Jan 29, 2017 at 4:50
  • I'm really curious why you posted a question about doing the opposite in Ruby just one minute before posting this one. Commented Jan 29, 2017 at 4:55
  • haha @Jordan quite good question. I'm learning both ruby and Javascript and as a part of that, I'm learning strings in both. Commented Jan 29, 2017 at 4:57
  • @NinjaBoy quite honestly you should have been able to research both easily. Research prior to asking questions here is expected Commented Jan 29, 2017 at 9:47

3 Answers 3

2

Pretty simple using Array.prototype.join()

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

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.