-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataConverter.js
41 lines (31 loc) · 925 Bytes
/
DataConverter.js
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
class DataConverter
{
/**
* function receives JSON object and converts the values (given as columns) to a csv format (as rows).
* Returns an array with all rows as strings
* @param data
*/
static toCsvFormat(data) {
let keys = Object.keys(data);
let colums = [];
let rows = [];
rows.push(keys.toString());
for (let key in data) {
colums.push(data[key])
}
let length = colums[0].length; // all columns are of same length
for (let i =0; i < length; i++) {
let tmp = ''
for (let col of colums) {
if (col === colums[colums.length-1]) {
tmp += col[i];
continue;
}
tmp += col[i] + ',';
}
rows.push(tmp);
}
return rows;
}
}
module.exports = DataConverter;