Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组到包含逗号的字符串';s-JavaScript_Javascript_Jquery_Arrays - Fatal编程技术网

数组到包含逗号的字符串';s-JavaScript

数组到包含逗号的字符串';s-JavaScript,javascript,jquery,arrays,Javascript,Jquery,Arrays,好的,我正在写一个包含完整句子的脚本,但是完整的句子可以包含逗号 由于脚本的工作方式,数组必须至少转换为字符串一次。 因此,当发生这种情况时,一旦我将字符串拆分回原始值,逗号就开始相互冲突 我不太明白如何解决这个问题,我一直在到处寻找,到目前为止都没有成功 我正在使用chrome插件,这是一个小例子: var Data = ["This is a normal string", "This string, will cause a conflict.", "This string should

好的,我正在写一个包含完整句子的脚本,但是完整的句子可以包含逗号

由于脚本的工作方式,数组必须至少转换为字符串一次。 因此,当发生这种情况时,一旦我将字符串拆分回原始值,逗号就开始相互冲突

我不太明白如何解决这个问题,我一直在到处寻找,到目前为止都没有成功

我正在使用chrome插件,这是一个小例子:

var Data = ["This is a normal string", "This string, will cause a conflict.", "This string should be normal"];

// The data gets sent to a background script, in string form and comes back as this
var Data = "This is a normal string, This string, will cause a conflict., This string should be normal";
Data = Data.split(",");

// Results in 4 positions instead of 3
Data = ["This is a normal string", "This string"," will cause a conflict.", "This string should be normal"];
在数组上调用
.join()
以将其转换为字符串时,可以指定分隔符

例如:

["Hello,","World"].join("%%%"); // "Hello,%%%World"
然后,您可以根据
“%%%”
.split(“%%”
)将其拆分,以便再次将其拆分

也就是说,如果您想对数组的每个元素(每行)应用一个操作,您可能不需要调用
.join
,然后再调用
.split
。相反,您可以使用数组方法,例如:

 var asLower = ["Hello","World"].map(function(line){ return line.toLowerCase(); });
 // as Lower now contains the items in lower case
或者,如果您的目标是序列化而不是处理-您不应该“滚动您自己的”序列化,而应该使用内置的
JSON.parse
JSON.stringify
方法,如H2Oooo建议的方法。

您可以使用并确保您输入的任何数组/对象都会返回完全相同的结果(它也适用于布尔、整数、浮点等):


您需要提供一些示例和当前代码。我们无法从您目前编写的代码中推断出任何内容。因此,不要使用逗号作为连接分隔符。join(">>>请详细解释为什么您的脚本需要字符串数组的字符串化。否则,只需转义逗号或使用JSON。如果其中一个句子包含短语
%%
?@Bergi+1当然,您必须选择一个您确定不会出现在字符串中的分隔符。这是一件值得注意的事情,谢谢。我还澄清了,这通常不是解决我认为的“Y”问题OP的首选方法(但我不知道)。
var data = ["This is a normal string", "This string, will cause a conflict.", "This string should be normal"];

// The data gets sent to a background script, in string form and comes back as this
data = JSON.stringify(data);
console.log(data);
// ["This is a normal string","This string, will cause a conflict.","This string should be normal"] 

data = JSON.parse(data);
console.log(data);
// ["This is a normal string", "This string, will cause a conflict.", "This string should be normal"]