gpt4 book ai didi

javascript - Javascript 数组中最后一项后的换行符

转载 作者:行者123 更新时间:2023-12-03 03:24:33 25 4
gpt4 key购买 nike

我有这段代码,它允许我使用二维数组下载 CSV 文件,但我需要在第一个数组中的最后一项之后有一个换行符。 (就在一些其他信息之后。

<script type="text/javascript">

// Example data given in question text
var data = [
['name1', 'city1', 'some other info'],
['name2', 'city2', 'more info']
];

// Building the CSV from the Data two-dimensional array
// Each column is separated by ";" and new line "\n" for next row
var csvContent = '';
data.forEach(function(infoArray, index) {
dataString = infoArray.join(';');
csvContent += index < data.length ? dataString + '\n' : dataString;
});

// The download function takes a CSV string, the filename and mimeType as parameters
// Scroll/look down at the bottom of this snippet to see how download is called
var download = function(content, fileName, mimeType) {
var a = document.createElement('a');
mimeType = mimeType || 'application/octet-stream';

if (navigator.msSaveBlob) { // IE10
navigator.msSaveBlob(new Blob([content], {
type: mimeType
}), fileName);
} else if (URL && 'download' in a) { //html5 A[download]
a.href = URL.createObjectURL(new Blob([content], {
type: mimeType
}));
a.setAttribute('download', fileName);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
location.href = 'data:application/octet-stream,' + encodeURIComponent(content); // only this mime type is supported
}
}

download(csvContent, 'dowload.csv', 'text/csv;encoding:utf-8');
</script>

所以要明确的是,我现在得到的结果是:

name1;city1;some other infoname2;city2;more info

我需要它是:

name1;city1;some other info
name2;city2;more info

提前非常感谢。

最佳答案

无论您使用什么方式显示文件,都可能需要 Windows 行结尾而不是 UNIX 行结尾。尝试 \r\n 而不仅仅是 \n

关于javascript - Javascript 数组中最后一项后的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46391385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com