Javascript 将多个字符串连接成一个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8733394/
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
Join multiple strings into one single string
提问by Zac
I have a loop that is generating a string
我有一个生成字符串的循环
function jsonResponse(response)
{
var singleString = a + "," + b + "," + c + "|";
}
with console.log(singleString);
和 console.log(singleString);
I see them all generated :
我看到它们都生成了:
a1,b1,c1|
a2,b2,c2|
a3,b3,c3|
But how can I create a new variable allStrings
that will concatenate all of these into one string? The loop is part of an ajax response that is looping through xml nodes to retrieve the data for those variables. I guess I need to make them part of an array and then join them back together for one big string?
但是如何创建一个allStrings
将所有这些连接成一个字符串的新变量?该循环是 ajax 响应的一部分,该响应通过 xml 节点循环以检索这些变量的数据。我想我需要让它们成为数组的一部分,然后将它们重新连接在一起形成一个大字符串?
To further clarify what I am trying to achieve is something like :
为了进一步澄清我想要实现的目标是这样的:
var allStrings = singleString[0] + singleString[1] + singleString[2] ;
a1,b1,c1|a2,b2,c2|a3,b3,c3|
To better explain the loop it looks like this :
为了更好地解释循环,它看起来像这样:
$j.ajax({
type: "GET",
url: "test.xml",
dataType: "xml",
success: function parseXml(data)
{
$j('.loader').fadeOut();
itemQueue = $j(data).find("ITEM").map( function ()
{
return {
date: $j("LAST_SCAN" , this).text(),
type : $j("PRODUCT_TYPE", this).text(),
cat : $j("CLASS_NAME", this).text(),
};
}).get();
getNextItem();
}
});
function getNextItem()
{
var item = itemQueue[0];
var singleString = item.date+ "," + item.type + "," + item.cat + "\n";
console.log( singleString );
$j.ajax({
url: s7query,
dataType: 'jsonp'
});
}
function s7jsonResponse(response)
{
var item = itemQueue.shift();
if (itemQueue.length)
{
getNextItem();
}
// run other processes when finished with checks
if (!itemQueue.length)
{
// alert ("ALL DONE");
}
}
回答by Rocket Hazmat
You can use Array.join
to convert an array to a string.
您可以使用Array.join
将数组转换为字符串。
Example:
例子:
var arr = ['a1', 'b1', 'c1'];
console.log(arr.join(',')); // 'a1,b1,c1'
回答by BeRecursive
You can get rid if that loop and use:
如果该循环并使用,您可以摆脱:
array.join(',')
Where array
is the array you want to turn in to a string separated by commas.
array
您要转换为以逗号分隔的字符串的数组在哪里。
To join the strings together you just need to construct a string concatenation:
要将字符串连接在一起,您只需要构造一个字符串连接:
allStrings.concat(string1, string2, ..., stringX)
回答by Brendan
It sounds like you're making separate XHRs for independent XML nodes, in which case you may want to reconsider your approach. If possible, I would iterate over the XML nodes in their entirety and gather all necessary request data. Then you can make a single XHR (cutting down on HTTP connections, which is good!) and get a single response that could contain the entirety of the response (i.e. allStrings). It should be easier on your server and make a noticable difference on the client side in pretty much all situations.
听起来您正在为独立的 XML 节点制作单独的 XHR,在这种情况下,您可能需要重新考虑您的方法。如果可能,我会遍历整个 XML 节点并收集所有必要的请求数据。然后,您可以创建一个 XHR(减少 HTTP 连接,这很好!)并获得一个可以包含整个响应(即 allStrings)的响应。它应该在您的服务器上更容易,并且在几乎所有情况下都会在客户端产生显着差异。