admin管理员组文章数量:1145653
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).
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).
Share Improve this question asked Jul 20, 2010 at 5:45 AlexAlex 77.3k91 gold badges264 silver badges350 bronze badges7 Answers
Reset to default 60var 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).
No need for jQuery
. Use Javascripts
join() method. Like
var arr = ["A12", "C105", "B50"],
str = arr.join('|');
alert(str);
Use JavaScript 'join
' method. Like this:
Array1.join('|')
Hope this helps.
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.
I using [email protected]. It's very good with array and object
_.join(['a', 'b', 'c'], '~');
// => 'a~b~c'
Ref lodash
var checked = $(':input[type="checkbox"]:checked').map(function(){return this.value}).get(); console.log(checked.join(", "));
<!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>
本文标签: jqueryTransform Javascript Array into delimited StringStack Overflow
版权声明:本文标题:jquery - Transform Javascript Array into delimited String - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737159413a1965096.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论