function
setup() {
createCanvas(600, 300);
textSize(18);
text(
"Click on the button to get the "
+
"values of the table as an array"
,
20, 20);
setBtn =
createButton(
"Get all table values"
);
setBtn.position(30, 40);
setBtn.mouseClicked(showTable);
table =
new
p5.Table();
setTableData();
}
function
setTableData() {
table.addColumn(
'Invention'
);
table.addColumn(
'Inventors'
);
let tableRow = table.addRow();
tableRow.setString(
'Invention'
,
'Telescope'
);
tableRow.setString(
'Inventors'
,
'Galileo'
);
tableRow = table.addRow();
tableRow.setString(
'Invention'
,
'Steam Engine'
);
tableRow.setString(
'Inventors'
,
'James Watt'
);
tableRow = table.addRow();
tableRow.setString(
'Invention'
,
'Radio'
);
tableRow.setString(
'Inventors'
,
'Guglielmo Marconi'
);
}
function
showTable() {
clear();
text(
"All values of the table are retrieved "
+
"using the getArray() method"
, 20, 20);
let tableArray = table.getArray();
console.table(tableArray);
for
(let r = 0; r < tableArray.length; r++) {
for
(let c = 0; c < tableArray[0].length; c++) {
text(str(tableArray[r]),
20 + 160 * c, 100 + 20 * r);
}
}
}