-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data.js
40 lines (38 loc) · 1.33 KB
/
Data.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
37
38
39
40
/**
* Turn a complex json response into a flat array of arrays. This can be
* used to populate, for example, a spreadsheet sheet or other tabular data.
*
* @param columns
* * Keys are spreadsheet headers.
* * Values are function callbacks that return the data from the json item.
* @param jsonArrayOfObjects
* * Iterable json array with each item being a consistently structured thing that the callbacks in columns know how to interact with.
*
* @return
* * A flattened array of arrays to go into a spreadsheet tab.
*/
function processJsonItemsWithCallbacks(columnsWithCallbacks, jsonArrayOfObjects) {
var processedData = [];
processedData.push(Object.keys(columnsWithCallbacks)); // Header row.
for (let i = 0, len = jsonArrayOfObjects.length; i < len; i++) {
let rowData = [];
for (let key in columnsWithCallbacks) {
let apply = columnsWithCallbacks[key];
// If 'apply' is callable, call it.
if (typeof apply === 'function') {
rowData.push(apply(key, jsonArrayOfObjects[i]));
}
// Otherwise assume it's a key.
else if (typeof apply === 'string') {
if (jsonArrayOfObjects[i]) {
rowData.push(jsonArrayOfObjects[i][apply]);
}
}
else {
rowData.push(null);
}
}
processedData.push(rowData);
}
return processedData;
}