37

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 this? I'm using jQuery (in case that helps with some kind of builtin function).

7 Answers 7

60
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).

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

Comments

11

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

var arr = ["A12", "C105", "B50"],
    str = arr.join('|');

alert(str);

Comments

1

Use JavaScript 'join' method. Like this:

Array1.join('|')

Hope this helps.

Comments

1

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.

Comments

0

I using [email protected]. It's very good with array and object

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

Ref lodash

Comments

-1

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

Comments

-1
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> 
<html> 
<head> 
<script> 
var employee = new Array(); 
employee.push("Yashwant"); 
employee.push("Dinesh"); 
employee.push("Mayur"); 
var employeeStr = employee.join("|"); 
alert('Delimited String :- ' + employeeStr); 
var employeeArray = new Array(); 
employeeArray = employeeStr.split("|"); 
for(var x=0;x<employeeArray.length;x++){ 
alert('Employee Name:- ' + employeeArray[x]); 
} 
</script>
</head> 
<body> 
</body> 
</html>

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.