Input
Summary
dictionary files
---
Date: 2022-03-03
---
Monitoring:: FundA | Do This0303
Monitoring:: FundA | Do That0303
Monitoring:: FundB | Do Something Else0303
---
Date: 2022-03-08
---
Monitoring:: FundM | Do This0308
Monitoring:: FundM | Do That0308
Monitoring:: FundN | Do Something Else0308
---
Date: 2022-04-03
---
Monitoring:: FundQ | Do This0403
Monitoring:: FundQ | Do That0403
Monitoring:: FundP | Do Something Else0403
---
Date: 2022-04-08
---
Monitoring:: FundR | Do This0408
Monitoring:: FundR | Do That0408
Monitoring:: FundS | Do Something Else0408
---
Date: 2022-05-03
---
Monitoring:: FundK | Do This0503
---
Date: 2022-05-08
---
Monitoring:: FundJ | Do Something Else0508
DQL10_split_flatten : For multiple Monitoring field(s) per file
Summary
Main DQL
Code Name |
Data type |
Purposes |
Remark |
DQL10_split_flatten |
a string or a list |
1.To get s_page.Fund_DQL and s_page.ActionItem_DQL from s_page.Monitoring
|
|
Code DQL10_split_flatten
````ad-info
title: DQL10_split_flatten =>For multiple Monitoring field(s) per file
collapse: close
icon:
color:
TABLE WITHOUT ID
file.link AS "File",
Monitoring AS "Monitoring",
split(Monitoring, "\s+\|\s+")[0] AS "Fund",
split(Monitoring, "\s+\|\s+")[1] AS "Action Item"
FROM "100_Project/02_dataview/Q06_Monitoring/Q06_test_data"
WHERE ( Monitoring != null )
SORT file.name ASC
FLATTEN Monitoring
````
Inline DQL
aMonitoring=[“FundA | Do This”, “FundA | Do That”]
a_string_splited==split("FundA | Do This", "\s+\|\s+")
//=>FundA, Do This
/\s+\|\s+/ ==>The spaces beside the "|" will be found and removed with "|" by `split`.
Screenshots(DQL10)
DQL20_split_flatten_filter : For multiple Monitoring field(s) per file
Summary
Main DQL
Code Name |
Data type |
Purposes |
Remark |
DQL20_split_flatten_filter |
a string or a list |
1.To filter by Monitoring 2.To exclude the display of some field contents |
|
Code DQL20_split_flatten_filter: final
````ad-info
title: DQL20_split_flatten_filter =>For multiple Monitoring field(s) per file 1.To filter by Monitoring 2.To exclude the display of some field contents
collapse: close
icon:
color:
TABLE WITHOUT ID
choice(
contains(Monitoring, "FundQ |"),
split(Monitoring, "\s+\|\s+")[0],
""
) as "Fund2",
choice(
contains(Monitoring, "FundQ |"),
split(Monitoring, "\s+\|\s+")[1],
""
) as "ActionItem2"
FROM "100_Project/02_dataview/Q06_Monitoring/Q06_test_data"
WHERE ( Monitoring != null )
AND ( contains(Monitoring, "FundQ |") )
SORT file.name ASC
FLATTEN Monitoring
````
Screenshots(DQL20)
DVJS30_flatten_filter
Summary
3.4.1. Main DVJS
Code Name |
Data type |
Purposes |
Remark |
DVJS30_flatten_filter |
a string or a list |
1.To flatten 2.To filter by dictionary.Fund |
|
3.4.2. Code DVJS30_flatten_filter
Summary_Code
title: DVJS30_flatten_filter =>To break up multiple group Dataview inline fields in the file into individual groups
collapse: close
icon:
color:
```dataviewjs
// M11. Let's gather all relevant pages: M11, M15
// #####################################################################
let pages = dv
.pages('"100_Project/02_dataview/Q06_Monitoring/Q06_test_data"')
.where((page) => page.Monitoring !== null);
// M23. Now let's transform from thing per Monitoring
// #####################################################################
let dictionaries = [];
for (let page of pages) {
// M23.IF01: Classify '[object Object]' into '[object Array]' and '[object notArray]'
// #####################################################################
let sObjectType = "";
if (dv.isArray(page.Monitoring)) {
sObjectType = "[object Array]";
} else {
sObjectType = "[object notArray]";
}
// M23.IF02: Define page.Schedul an array + Redefine sObjectType
// #####################################################################
if (sObjectType === "[object notArray]") {
page.Monitoring = [page.Monitoring];
sObjectType = "[object Array]";
}
// M23.EL03: Transform the data when page.Monitoring is an array
// #####################################################################
for (let i = 0; i < page.Monitoring.length; i++) {
dictionaries.push({
file: page.file,
date: page.date,
Monitoring: page.Monitoring[i],
Fund: page.Monitoring[i].replace(/\s+\|\s+.+$/, ""),
ActionItem: page.Monitoring[i].replace(/^.+\s+\|\s+/, ""),
});
}
}
// M24. dictionaries: JS arrays to Data Arrays to be sorted
// #####################################################################
dictionaries = dv.array(dictionaries);
// M25. sort it by Fund in ascending order:
// M31. Let's filter it by dictionary.X : M31,M33
// #####################################################################
dictionaries = dictionaries
.sort((dictionary) => dictionary.Fund, 'asc')
.filter(
(dictionary) =>
dv.func.contains(dictionary.Fund, "FundQ")
);
// M41. And let's print a table of the 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,
])
);
```
3.5. Screenshots(DVJS30)
4. Explanation: DVJS30_flatten_filter
Summary_Explanation
4.1. Overview
4.1.1. Main variables
- The following table presents the main variables in the Code Main.
Variables |
Data type |
Lifetime |
Declared by |
Remark |
pages |
Array |
When the code is completed |
let |
Array of Hashes |
page |
Hash |
In the {} curly brackets |
|
|
dictionaries |
Array |
When the code is completed |
let |
Array of Hashes |
dictionary |
Hash |
In the {} curly brackets |
|
|
4.1.1.1. 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
.
4.1.1.2. 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.
{
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'],
}
4.2. 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 is to be taken into consideration.
// M11. Let's gather all relevant pages: M11, M15
// #####################################################################
let pages = dv
.pages('"100_Project/02_dataview/Q06_Monitoring/Q06_test_data"')
.where((page) => page.Monitoring !== null);
4.3. Step M23: break up
- 【M23】: Break 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).
4.3.1. Take dic_20220303
for example: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.
4.3.1.1. 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.
{
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.
4.3.1.2. 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.
{
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.
{
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.
{
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: JS arrays to Data Arrays to be sorted
// M24. dictionaries: JS arrays to Data Arrays to be sorted
// #####################################################################
dictionaries = dv.array(dictionaries);
4.4. Step M25: sort by dictionary.Fund
- 【M25】: Sort the
dictionaries
array by the dictionary.Fund
in ascending order.
4.5. Step M31: filter by dictionary.Fund
- 【M31】: Suppose that the row where
dictionary.Fund
includes “FundQ” is to be taken into consideration.
- Here is the code.
// M25. sort it by Fund in ascending order:
// M31. Let's filter it by dictionary.X : M31,M33
// #####################################################################
dictionaries = dictionaries
.sort((dictionary) => dictionary.Fund, 'asc')
.filter(
(dictionary) =>
dv.func.contains(dictionary.Fund, "FundQ")
);
4.6. Step M41: TABLE
- 【M41】: Display each of the list as a table.
[
index + 1,
dictionary.file.link,
dictionary.Monitoring,
dictionary.Fund,
dictionary.ActionItem,
]
// M41. And let's print a table of the 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,
])
);
4.6.1. Example: Get name from AoH
a_Drinks=$= [ { name: "Black Coffee", price: 120, "caffeine content":300 }, { name: "Green Tea", price: 100, "caffeine content":200}, { name: "Apple Juice", price: 110, "caffeine content":0 }, { name: "Iced Chocolate", price: 130, "caffeine content":0 }, { name: "Hot Chocolate", price: 105, "caffeine content":0 }, ] .map((e) => e["name"])
//=>Black Coffee, Green Tea, Apple Juice, Iced Chocolate, Hot Chocolate
DVJS40_Output_aoh_drinks
Summary_Example
5.1.1. Main DVJS
Code Name |
Data type |
Purposes |
Remark |
DVJS40_Output_aoh_drinks |
array of hashes |
1.To filter by h_drink.name 2.To filter by h_drink.price 3.To filter by h_drink[“caffeine content”] 4.To sort by h_drink.name 5.To display as a table |
|
5.1.2. Code DVJS40_Output_aoh_drinks
title: DVJS40_Output_aoh_drinks: Output aoh_drinks (array of hashes)
collapse: close
icon:
color:
```dataviewjs
// ### D21.define : aoh_drinks (array of hashes) ###
// #####################################################################
let aoh_drinks = [
{ name: "Black Coffee", price: 120, "caffeine content": 300 },
{ name: "Green Tea", price: 100, "caffeine content": 200 },
{ name: "Apple Juice", price: 110, "caffeine content": 0 },
{ name: "Iced Chocolate", price: 130, "caffeine content": 0 },
{ name: "Hot Chocolate", price: 105, "caffeine content": 0 },
];
// ### D21.aoh_drinks : JS arrays to Data Arrays to be sorted
// #####################################################################
aoh_drinks = dv.array(aoh_drinks);
// ### D31.aoh_drinks: filter + sort
// #####################################################################
aoh_drinks = aoh_drinks
.filter(
(h_drink) =>
dv.func.contains(h_drink.name, "Coffee") ||
dv.func.contains(h_drink.name, "Tea") ||
dv.func.contains(h_drink.name, "Juice") ||
dv.func.contains(h_drink.name, "Chocolate")
)
.filter((h_drink) => h_drink.price > 90 && h_drink.price < 125)
.filter(
(h_drink) => h_drink["caffeine content"] >= 0 && h_drink.price <= 300
)
.sort((h_drink) => h_drink.name, "asc");
// ### D41.Output : aoh_drinks ###
// #####################################################################
dv.header(5, "D41.Print a table of the `aoh_drinks`");
dv.table(
["N", "Name", "Price", "Caffeine Content"],
aoh_drinks.map((dictionary, index) => [
index + 1,
dictionary.name,
dictionary.price,
dictionary["caffeine content"],
])
);
```
5.2. Screenshots(DVJS40)
Reference
Summary
- DataviewJS Snippet Showcase: multiple group Dataview inline fields in the file
- How to break up multiple group Dataview inline fields in the file into individual groups?
- DataviewJS Snippet Showcase: multiple group YAML fields in the file
- How to break up multiple group YAML fields in the file into individual groups?