-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson-csv.js
More file actions
47 lines (38 loc) · 775 Bytes
/
json-csv.js
File metadata and controls
47 lines (38 loc) · 775 Bytes
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
const getSchema = data => {
const schema = [];
if (Array.isArray(data)) {
data.forEach(row => {
Object.keys(row).forEach(key => {
if (schema.indexOf(key) === -1) {
schema.push(key);
}
});
});
}
return schema;
};
const generateResultSet = (data, schema) => {
const resultSet = [];
data.forEach(row => {
const resultRow = [];
schema.forEach(key => {
resultRow.push(row[key] || "");
});
resultSet.push(resultRow);
});
return resultSet;
};
// how to deal with nested structures
const data = [
{
code: 1234,
name: "abcd"
},
{
code: 5678,
name: "pqrs"
}
];
const schema = getSchema(data);
const resultSet = generateResultSet(data, schema);
console.log(resultSet);