簡體   English   中英

將字符串數組轉換為 csv 的字符串

[英]Convert a array of strings into string for the csv

我有以下字符串數組。

 const test = ["Date,type,user,Account,Desc\r",
    "1 jan 2021,RR,GK,,Testing acc\r",
    "1 jan 2021,RR,GK,,Testing acc\r",
    "1 jan 2021,RR,GK,,Testing acc\r",
    "1 jan 2021,RR,GK,,Testing acc\r"]

現在我正在嘗試將這個字符串數組轉換為單個字符串,然后可以將其用於 CSV 創建。

所以我嘗試test.toString()導致

"Date,type,user,Account,Desc
,1 jan 2021,RR,GK,,Testing acc
,1 jan 2021,RR,GK,,Testing acc
,1 jan 2021,RR,GK,,Testing acc
,1 jan 2021,RR,GK,,Testing acc
"
After using this data for CSV creation , it is actually skipping one column means Date date gets added in the next column and last data which is Testing acc does not contain any header it should have been Desc.

如果我在1 jan 2021 ,那么它會正確獲取所有列,但日期應該是######而不是原始值。

我該如何解決?

謝謝

嘗試使用:

const separator = '\n'; // or select enother value
test.join(separator)

加入方法文檔: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM