Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

javascript - How to add new line in CSV generated by jQuery?

I have a list that needs to be generated as a CSV. Here it is:

[101, true, P, b, br, 2020], [101, true, P, 20, br, 2020], [101, true, P, b, breach, 2020]

And I wrote this to convert it:

var link = document.createElement('a');
link.id = 'download-csv';
link.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(queries.toString().replaceAll("[","").replaceAll("],","\r\n").replaceAll(",",";").trim().replaceAll("]","")));
link.setAttribute('download', 'yourfiletextgoeshere.csv');
document.body.appendChild(link);
document.querySelector('#download-csv').click();

And generated a CSV file is like this:

101; true; P; b; br; 2020
101; true; P; 20; br; 2020
101; true; P; b; br; 2020

Instead of breaking the line, it continues as if is a string.

question from:https://stackoverflow.com/questions/65897660/how-to-add-new-line-in-csv-generated-by-jquery

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the comment by user freedomn-m, which OP responded 'solved' their problem:

Use this:

.replaceAll("],","\r\n") -> .replaceAll("],"," ")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...