The flat()
function flattens multiple lists into a single list.
flat(list)
list.flat()
Code language: JavaScript (javascript)
Note that unlike the flat()
function in JavaScript, you cannot specify the depth. Notion’s version will only flatten one “level” of lists.
When working with flat()
and other list functions, I highly recommend clicking the Show Details icon. This will show the data type of your formula’s final return value, and it’ll also show the brackets that surround elements in any lists.
This is particularly useful when working with flat()
, since you’ll likely be working with one or more nested lists.
Example Formulas
flat([[1, 2], [3, 4]]) /* Output: [1, 2, 3, 4] */
[
[ "Luffy", "Sanji"],
[ "Nami", "Usopp"],
[ "Chopper" ]
].flat()
/* Output: [Luffy, Sanji, Nami, Usopp, Chopper] */
Code language: JavaScript (javascript)
Here’s a case where you might use flat()
– if you had a collection of multiple lists, you might want to find the element amongst all of them with the highest character count:
let(
teams,
[
[ "Luffy", "Zoro", "Nami", "Usopp", "Sanji"],
[ "Goku", "Vegeta", "Krillin"],
[ "Iron Man", "Hulk", "Thor", "Captain America", "Black Widow", "Hawkeye"]
],
teams.flat().sort(
current.length()
).last()
)
/* Output: Captain America */
Code language: JavaScript (javascript)