我有將 json 匯出到 txt 檔案的 Javascript 函式
function download() {
const originalData = [
{
name: 'Support',
message: 'Hey! Welcome to support',
},
{
name: 'Me',
message: 'Hello there!',
},
{
name: 'Support',
message: 'What can I do for you?',
},
];
var items = originalData;
const replacer = (key, value) => (value === null ? '' : value); // specify how you want to handle null values here
const header = Object.keys(items[0]);
let csv = items.map((row) =>
header
.map((fieldName) => JSON.stringify(row[fieldName], replacer))
.join(',')
);
csv.unshift(header.join(','));
csv = csv.join('\r\n');
//Download the file as CSV/TXT just change the extension
var downloadLink = document.createElement('a');
var blob = new Blob(['\ufeff', csv]);
var url = URL.createObjectURL(blob);
downloadLink.href = url;
downloadLink.download = 'DataDump.txt'; //Name the file here
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
結果是
name,message
"Support","Hey! Welcome to support"
"Me","Hello there!"
"Support","What can I do for you?"
我想要的預期值是這樣的,在行 > 1 的每個最后一個字串中都包含 '*'
?name,message
"Support","Hey! Welcome to support"*
"Me","Hello there!"*
"Support","What can I do for you?"*
如何在行 > 1 或行 > 1 上添加“*”
uj5u.com熱心網友回復:
最簡單的可能是,只需*在創建 CSV 行時添加
let csv = items.map((row) =>
header
.map((fieldName) => JSON.stringify(row[fieldName], replacer))
.join(',') "*"
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/346287.html
標籤:javascript
