Skip to content

Commit 626bdd9

Browse files
committed
Code tidying
1 parent d122a23 commit 626bdd9

File tree

1 file changed

+19
-34
lines changed

1 file changed

+19
-34
lines changed

generic.html

+19-34
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
<script>
77

88
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+
}
1818

1919
function dodrop(event) {
2020
var dt = event.dataTransfer;
@@ -25,15 +25,12 @@
2525
for (var i = 0; i < files.length; i++) {
2626
reader = new FileReader();
2727
reader.onload = function (event) {
28-
29-
30-
28+
3129
const input = event.target.result;
3230
let result = input.split('\n').map(function(s) { if (s) { return JSON.parse(s); } });
3331

3432
const replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values here
3533

36-
//const header = Object.keys(result[0]);
3734
const header = {}
3835
flatten(result[0], header)
3936
console.log(header);
@@ -42,29 +39,17 @@
4239
if (result[result.length - 1] == undefined) {
4340
result.pop();
4441
}
45-
46-
//result.forEach(function (rows) {
4742

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;
5252

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-
// });
6853
}
6954
reader.readAsText(files[i]);
7055
}

0 commit comments

Comments
 (0)