Repository navigation
Expand file tree
/
Copy pathpivoty.js
More file actions
67 lines (60 loc) · 1.83 KB
/
Copy pathpivoty.js
File metadata and controls
67 lines (60 loc) · 1.83 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module.exports = {
// take the mongo json data and turn it into a simple pivot
// we get a list of all the headers that only contain string/number
// types
pivot : function(data) {
var headers = this.deriveHeaders(data);
return this.deriveTable(headers, data);
},
//scan through the data and only use simplevalues
deriveHeaders : function(data) {
var headers = {};
for (var i=0; i!=data.length;i++) {
for (var key in data[i]) {
// we're assuming here that every element with a given name
// is of the same type - we can do this here because
// we know the data
if (data[i].hasOwnProperty(key) && !headers[key]) {
var sampleValue = data[i][key];
var header = {
'key' : key,
'sampleValue' : sampleValue,
'valueType' : typeof sampleValue
}
if(header.valueType === 'string' || header.valueType === 'number') {
headers[key]=header;
}
}
}
}
return headers;
},
deriveTable: function(headers, data) {
var table = [];
// get the header row
var headerRow= [];
for (var key in headers) {
if (headers.hasOwnProperty(key)) {
headerRow.push(key);
}
}
headerRow.sort();
table.push(headerRow);
//get data as a table
for (var i=0; i!= data.length; i++) {
var row =[],
source = data[i];
for (var j=0; j!=headerRow.length; j++) {
var headerCol=headerRow[j];
var value ='';
if (source.hasOwnProperty(headerCol)) {
value = source[headerCol];
}
//console.log('writing row ['+i+']['+dkey+']=['+source[dkey]+']');
row.push(value);
}
table.push(row);
}
return table;
}
}