-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpure_js_array_to_csv.js
More file actions
53 lines (52 loc) · 1.49 KB
/
pure_js_array_to_csv.js
File metadata and controls
53 lines (52 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Declare the Array
var all_csv_columns =
[
[
"Column1",
"Column2",
]
];
//Append to array
all_csv_columns.push([
data1,
data2,
])
//Parse the array into a csv content
var csvContent = '';
for(i=0; i<all_csv_columns.length; i++){
var value = all_csv_columns[i];
for (var j = 0; j < value.length; j++) {
var innerValue = value[j];
var result= "";
if(innerValue){
result = (innerValue.toString()).replace(/"/g, '""');
}
if (result.search(/("|,|\n)/g) >= 0){
result = '"' + result + '"';
}
if (j > 0){
csvContent += ',';
}
csvContent += result;
}
csvContent += '\n';
};
//Download the file into their local machine
var filename = "somefile"+".csv";
//\uFFFF to accept French Characters
var blob = new Blob(["\uFEFF", csvContent], { type: 'text/csv;charset=utf-8;' });
if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, filename);
} else {
var link = document.createElement("a");
if (link.download !== undefined) { // feature detection
// Browsers that support HTML5 download attribute
var url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
};