Hi,
I have a 2d array defined in a json file with following structure
[
[
"Date",
"Tran Type",
"Tran ID",
"Withdrawals",
"Deposits"
],
[
"01/05/2024",
"TFR",
"S81420904",
"700.00",
""
],
[
"01/05/2024",
"TFR",
"S84554541",
"",
"1.01"
]
]
I am looking to format this as a table. The first row is the header and after that it’s data rows. The attempt I have made is as follows:
#let txn = json("data1.json")
#txn.at(0)
#let to_content(r) = [#r.at(0) #r.at(1) #r.at(2) #r.at(3) #r.at(4)]
#table(
columns: 5,
to_content(..txn)
)
Which obviously is not working. Is there a means to format the 2d array as a table directly?
Use array.flatten()
:
#let txn = json("data1.json")
#table(
columns: 5,
..txn.flatten()
)
1 Like
Thanks! Its working fine.
Just a followup question about how flatten()
work here. I can understand that the ..
spread operator converts the resultent arraay into individual rows that can be used by #table
. But I could not yet fully understand the flatten()
operator. The table expects (in my faulty understanding) data in the form of a row, which is what each row of ..txn
provides, what additional facility does flatten()
provides?
The reference also do not shed much light into the implication of flatten()
Combine all nested arrays into a single flat one.
Thanks!
Thanks for the helpful explanation! Couple of questions, if I may:
In ..array.flatten()
in which order does the operations happen? ie does the flatten()
happen first or does the ..
spread happen first?
- If
flatten()
happen first, then how does the ..
knows how to split the array into rows, because it will be just one big 1d array.
- If
..
happen first then we have a collection of 1d arrays, and the flatten()
operator do not have a multi-dimentional array to work with. In this case, will it just return a just a collection of content?
flatten()
happens first.
..
DOES NOT split array. It just unpacks. table
splits providing cells into rows based on columns
argument.
1 Like
Thanks, that clarifies it!