javascript在数据中使用逗号转换csv的字符串

时间:2015-11-20 06:53:49

标签: javascript csv encoding

我正在生成列名为namedescription等的动态csv文件。在description字段中有逗号,所以当我打开csv文件时会打破列。我需要用包含的双引号转换字符串。

例如:

name, description, price
SSD, Solid state drive, 100
keyboard,some description with comma, 20

我需要用双引号转换字符串

name, description, price
"SSD", "Solid state drive", 100
"keyboard" ,"some description with comma", 20

这里是我用来转换csv的代码:

_.each(product, function(obj){
    csvContent += obj.name + "," +obj.desc + "," + obj.price + "\n"
}

我觉得手动添加双引号并不好。是否有任何函数可以将双引号添加到字符串?

0 个答案:

没有答案