How could i convert array into string and separate values by pipe in javascript (es6)? For example, ["one", "two", "three"]
should be converted into "one|two|three"
Asked
Active
Viewed 1,967 times
-1

Alex Monroe
- 3
- 3
-
6https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join – CertainPerformance May 05 '18 at 04:51
-
Not sure that ES6 is necessary, but if you _really_ wanted to use an ES6 feature to do this, you could treat `.join()` as a template tag, e.g. ``elements.join`|` `` – Patrick Roberts May 05 '18 at 05:03
4 Answers
2
var elements = ["one", "two", "three"];
console.log(elements.join('|'));
// expected output: one|two|three

Tariq Rasheed
- 158
- 7
0
Try this:
let myArray = ["one", "two", "three"];
let myString = myArray.join("|");
console.log(myString);

Abe
- 1,357
- 13
- 31
0
var elements = ["one", "two", "three"];
var pipe_delimited_string = elements.join("|");
console.log(pipe_delimited_string);

dayan khan
- 51
- 3
0
The Join function is used to convert array to string and split('') function is used to convert string into array i.e
var arrayData = ['data 1', 'data 2', 'data 3'];
console.log(arrayData.join('|'));//bydefault it split with ','
var astringyData = 'data 1|data 2|data 3';
console.log(astringyData.split('|'));

Faraz Babakhel
- 654
- 5
- 14