Summary
Overview
Main variables
- The following table presents the main variables in the DVJS03.
- a Hash = an JavaScript Object
Variables |
Data type |
Lifetime |
Declared by |
Remark |
pages |
Array |
When the code is completed |
let |
AoH = an Array of Hashes |
page |
Hash |
In the {} curly brackets |
|
|
dictionaries |
Array |
When the code is completed |
let |
AoH = an Array of Hashes |
dictionary |
Hash |
In the {} curly brackets |
|
|
The data structure of the pages
- The
pages
array consists of each page
hash.
- The
page
is pages[i]
, where i=0, …, pages.length-1
.
The data structure of the page
- Take the
dic_20220303
file for example.
- filename :
dic_20220303
---
Date: 2022-03-03
---
Monitoring:: FundA | Do This0303
Monitoring:: FundA | Do That0303
Monitoring:: FundB | Do Something Else0303
- The plain
page
hash from a file is created by the dv.pages()
function and consists of three hashes : the page.file
hash, the YAML fields and the Dataview inline fields.
- Here is a slice of the
page
hash, where page.file.name
is dic_20120919
.
- At Step M23, the following hash is transformed into two hashes, which are stored in the
dictionaries
array.
```JSON
{
file: {
name: 'dic_20220303',
ctime: '2022-03-03T19:30:50.586+08:00',
},
date: '2022-03-03T00:00:00.000+08:00',
Monitoring: ['FundA | Do This0303', 'FundA | Do That0303', 'FundB | Do Something Else0303'],
}
```
Step M11: Sources
- 【M11】: Suppose that there are many files located in the path
100_Project/02_dataview/Q06_Monitoring/Q06_test_data
.
- 【M15】: Suppose that the file where
page.Monitoring
does not strictly equal null and undefined is to be taken into consideration.
```dataviewjs
// M11. define pages: get pages from Sources (M11, M15)
// #####################################################################
let pages = dv
.pages('"100_Project/02_dataview/Q06_Monitoring/Q06_test_data"')
.where((page) => page.Monitoring !== null && page.Monitoring !== undefined);
```
Step M23: Split up
- 【M23】: Split up multiple group Dataview inline fields in the file into individual groups. (
pages
, page
==> dictionaries
, dictionary
)
- Now let’s transform from thing per date (we have single page for each date) into thing per dictionary (we want to have as many rows in a table as all dictionaries extracted from all pages).
- The
dictionary
is a new row(hash) which consists of five fields sush as page.file
,page.date
,page.Monitoring
, page.Fund
,and page.ActionItem
.
- The
dictionaries
is a new table(array), which consists of each dictionary
row(hash).
Take the dic_20220303
for example:The Monitoring
is an array
---
Date: 2022-03-03
---
Monitoring:: FundA | Do This0303
Monitoring:: FundA | Do That0303
Monitoring:: FundB | Do Something Else0303
- There is ==more== than one
Monitoring
in the original page
file.
the original data structure
-
The plain page
hash from a file is created by the dv.pages()
function and consists of three hashes : the page.file
hash, the YAML fields and the Dataview inline fields.
-
At Step M23, the following hash is transformed into three hashes, which are stored in the dictionaries
array.
-
Here is a slice of the page
hash from the dic_20220303
file.
```JSON
{
file: {
name: 'dic_20220303',
ctime: '2022-03-03T19:30:50.586+08:00',
},
date: '2022-03-03T00:00:00.000+08:00',
Monitoring: ['FundA | Do This0303', 'FundA | Do That0303', 'FundB | Do Something Else0303'],
}
```
- The
page
hash is transformed into three new hashes as follows.
the new data structure
- The
dictionaries
array consists of each dictionary
hash.
- The plain
dictionary
hash is created from the pages
at Step M23 and consists of three hashes : the page.file
hash, the YAML fields and the Dataview inline fields.
- At Step M23, the following hashes are created from the above
page
hash.
- Here’s one
dictionary
hash, which the dictionaries
array consists of.
```JSON
{
file: {
name: 'dic_20220303',
ctime: '2022-03-03T19:30:50.586+08:00',
},
date: '2022-03-03T00:00:00.000+08:00',
Monitoring: 'FundA | Do This0303',
Fund: 'FundA'
ActionItem: 'Do This0303'
}
```
- Here’s another
dictionary
hash, which the dictionaries
array consists of.
```JSON
{
file: {
name: 'dic_20220303',
ctime: '2022-03-03T19:30:50.586+08:00',
},
date: '2022-03-03T00:00:00.000+08:00',
Monitoring: 'FundA | Do That0303',
Fund: 'FundA'
ActionItem: 'Do That0303'
}
```
- Here’s the other
dictionary
hash, which the dictionaries
array consists of.
```JSON
{
file: {
name: 'dic_20220303',
ctime: '2022-03-03T19:30:50.586+08:00',
},
date: '2022-03-03T00:00:00.000+08:00',
Monitoring: 'FundB | Do Something Else0303',
Fund: 'FundB'
ActionItem: 'Do Something Else0303'
}
```
Step M24: transform JavaScript arrays to Dataview Data arrays
```dataviewjs
// M24. transform dictionaries:
// transform JavaScript arrays to Dataview Data arrays in order to sort it
// #####################################################################
dictionaries = dv.array(dictionaries);
```
Step M25: sort by dictionary.Fund
- 【M25】: Sort the
dictionaries
array by the dictionary.Fund
in ascending order.
Step M31: filter by dictionary.Fund
- 【M31】: Suppose that the row where
dictionary.Fund
includes “FundQ” is to be taken into consideration.
- 【M33】: Suppose that the row where
dictionary.ActionItem
includes “That” is to be taken into consideration.
- Here is the code.
```dataviewjs
// M25. sort by dictionary.Fund in ascending order: M99.(at the bottom)
// M31. filter by dictionary.Fund
// M33. filter by dictionary.ActionItem
// #####################################################################
dictionaries = dictionaries
.sort((dictionary) => dictionary.Fund, "asc")
.filter((dictionary) => dv.func.contains(dictionary.Fund, "FundQ"))
.filter((dictionary) => dv.func.contains(dictionary.ActionItem, "That"));
```
Step M41: TABLE
- 【M41】: Display each of the list as a table.
```javascript
[
index + 1,
dictionary.file.link,
dictionary.Monitoring,
dictionary.Fund,
dictionary.ActionItem,
]
```
```dataviewjs
// M41. render a table: dictionaries
// #####################################################################
dv.header(5, "M41. Print a table of the `dictionaries`");
dv.table(
["N", "File", "Monitoring", "Fund", "Action Item"],
dictionaries.map((dictionary, index) => [
index + 1,
dictionary.file.link,
dictionary.Monitoring,
dictionary.Fund,
dictionary.ActionItem,
])
);
```
Example: Get each name from an AoH
```md
a_Drinks=`$=
[
{ item_name: "Black Coffee", unit_price: 120, "caffeine content": 300, item_no: "IT001" },
{ item_name: "Green Tea", unit_price: 100, "caffeine content": 200, item_no: "IT002" },
{ item_name: "Apple Juice", unit_price: 110, "caffeine content": 0, item_no: "IT003" },
{ item_name: "Iced Chocolate", unit_price: 130, "caffeine content": 0, item_no: "IT004" },
{ item_name: "Hot Chocolate", unit_price: 105, "caffeine content": 0, item_no: "IT005" },
]
.map((e) => e["item_name"])
`
//=>Black Coffee, Green Tea, Apple Juice, Iced Chocolate, Hot Chocolate
```