-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseTable.js
35 lines (30 loc) · 1005 Bytes
/
parseTable.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
function mapRow(headings) {
return function mapRowToObject({ cells }) {
return [...cells].reduce(function(result, cell, i) {
const input = cell.querySelector("input,select");
let value;
if (input) {
value = input.type === "checkbox" ? input.checked : input.value;
} else {
value = cell.innerText;
}
return Object.assign(result, { [headings[i]]: value });
}, {});
};
}
function getHeadings(table) {
return [...table.tHead.rows[0].cells].map(
heading => heading.innerText
);
}
/**
* given a table, generate an array of objects.
* each object corresponds to a row in the table.
* each object's key/value pairs correspond to a column's heading and the row's value for that column
*
* @param {HTMLTableElement} table the table to convert
* @return {Array<Object>} array of objects representing each row in the table
*/
function parseTable(table) {
return [...table.tBodies[0].rows].map(mapRow(getHeadings(table)));
}