将 Javascript 数组转换为分隔字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3287314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transform Javascript Array into delimited String
提问by Alex
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...
我有一个带有 A12、B50、C105 等值的 Javascript 字符串数组,我想把它变成一个像这样的管道分隔字符串:A12|B50|C105...
How could I do this? I'm using jQuery (in case that helps with some kind of builtin function).
我怎么能这样做?我正在使用 jQuery(以防它对某种内置函数有帮助)。
回答by Daniel Vandersluis
var pipe_delimited_string = string_array.join("|");
Array.joinis a native Arraymethod in Javascript which turns an array into a string, joinedby the specified separator (which could be an empty string, one character, or multiple characters).
Array.join是一个本地Array的Javascript方法果然阵列成一个字符串,加入由指定分离器(它可以是一个空字符串,一个字符或多个字符)。
回答by jAndy
No need for jQuery. Use Javascriptsjoin()method. Like
不需要jQuery。使用Javascriptsjoin()方法。喜欢
var arr = ["A12", "C105", "B50"],
str = arr.join('|');
alert(str);
回答by NawaMan
Use JavaScript 'join' method. Like this:
使用 JavaScript ' join' 方法。像这样:
Array1.join('|')Hope this helps.
希望这可以帮助。
回答by Garett
For a native JavaScript array then myArray.join('|')will do just fine.
对于原生 JavaScript 数组myArray.join('|'),就可以了。
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):
另一方面,如果您使用的是 jQuery 并且返回值是一个 jQuery 包装的数组,那么您可以执行以下操作(未经测试):
jQuerySelectedArray.get().join('|')
See this articlefor more information.
有关更多信息,请参阅此文章。
回答by Tính Ng? Quang
I using [email protected]. It's very good with array and object
我使用[email protected]。数组和对象非常好
_.join(['a', 'b', 'c'], '~');
// => 'a~b~c'
回答by Sam Deering
var checked = $(':input[type="checkbox"]:checked').map(function(){return this.value}).get(); console.log(checked.join(", "));
var 检查 = $(':input[type="checkbox"]:checked').map(function(){return this.value}).get(); console.log(checked.join(", "));
回答by Vicky
<!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>

