|
6 | 6 | <script>
|
7 | 7 |
|
8 | 8 | function flatten(object, target, path) {
|
9 |
| - path = path || ''; |
10 |
| - Object.keys(object).forEach(function (key) { |
11 |
| - if (object[key] && typeof object[key] === 'object') { |
12 |
| - flatten(object[key], target, path + key); |
13 |
| - return; |
14 |
| - } |
15 |
| - target[path + key] = object[key]; |
16 |
| - }); |
17 |
| -} |
| 9 | + path = path || ''; |
| 10 | + Object.keys(object).forEach(function (key) { |
| 11 | + if (object[key] && typeof object[key] === 'object') { |
| 12 | + flatten(object[key], target, path + key); |
| 13 | + return; |
| 14 | + } |
| 15 | + target[path + key] = object[key]; |
| 16 | + }); |
| 17 | + } |
18 | 18 |
|
19 | 19 | function dodrop(event) {
|
20 | 20 | var dt = event.dataTransfer;
|
|
25 | 25 | for (var i = 0; i < files.length; i++) {
|
26 | 26 | reader = new FileReader();
|
27 | 27 | reader.onload = function (event) {
|
28 |
| - |
29 |
| - |
30 |
| - |
| 28 | + |
31 | 29 | const input = event.target.result;
|
32 | 30 | let result = input.split('\n').map(function(s) { if (s) { return JSON.parse(s); } });
|
33 | 31 |
|
34 | 32 | const replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values here
|
35 | 33 |
|
36 |
| - //const header = Object.keys(result[0]); |
37 | 34 | const header = {}
|
38 | 35 | flatten(result[0], header)
|
39 | 36 | console.log(header);
|
|
42 | 39 | if (result[result.length - 1] == undefined) {
|
43 | 40 | result.pop();
|
44 | 41 | }
|
45 |
| - |
46 |
| - //result.forEach(function (rows) { |
47 | 42 |
|
48 |
| - //const header = Object.keys(result[0]); |
49 |
| - //const row = {} |
50 |
| - //flatten(result, row) |
51 |
| - |
| 43 | + const csv = [ |
| 44 | + Object.keys(header).join(','), // header row first |
| 45 | + ...Object.values(result).map(function(r) { |
| 46 | + const row = {} |
| 47 | + flatten(r, row) |
| 48 | + return Object.keys(header).map(fieldName => JSON.stringify(row[fieldName]) ).join(',') |
| 49 | + }) |
| 50 | + ].join('\r\n') |
| 51 | + document.getElementById('csv').innerText = csv; |
52 | 52 |
|
53 |
| - //Object.keys(header).forEach(function(h) { lines.push(row[h]) } ) |
54 |
| - /*const csv = [ |
55 |
| - Object.keys(header).join(','), // header row first |
56 |
| - Object.values(row).join('\n') |
57 |
| - ].join('\n')*/ |
58 |
| - const csv = [ |
59 |
| - Object.keys(header).join(','), // header row first |
60 |
| - ...Object.values(result).map(function(r) { |
61 |
| - const row = {} |
62 |
| - flatten(r, row) |
63 |
| - return Object.keys(header).map(fieldName => JSON.stringify(row[fieldName]) ).join(',') |
64 |
| - }) |
65 |
| - ].join('\r\n') |
66 |
| - document.getElementById('csv').innerText = csv; |
67 |
| - // }); |
68 | 53 | }
|
69 | 54 | reader.readAsText(files[i]);
|
70 | 55 | }
|
|
0 commit comments