dv.table() for custom arrays or maps #1411
-
I am having a hard time figuring out how dv.table() works based on the documentation (it's me, not the docs!). The part that I struggle with is this:
I don't have a programming background, so I am not entirely clear what the
What has stumped me so far is how to adapt this code to an object that exists within the dataviewjs, e.g.
Returns In an ideal world, I would use a table to return a map of objects, e.g. something along the lines of:
What am I missing? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
Beta Was this translation helpful? Give feedback.
-
If your input is an array of maps, like this: const results = [
{name: "abc", height: "3", diameter: "2"},
{name: "efg", height: "7", diameter: "4"},
];
dv.table(["name", "height", "diameter"], results.map(r => [r.name, r.height, r.diameter])); This is the standard usage of const map = new Map([
[0, { name: "hello" }],
[1, { name: "goodbye" }]
]);
dv.table(["name"], dv.array(Array.from(map)).sort(([index, _]) => index)
.map(([key, value]) => [value.name])); |
Beta Was this translation helpful? Give feedback.
dv.table()
takes in an array of arrays, and formats it as a table. It does not support rendering maps, though you can convert a map to an array without too much trouble.If your input is an array of maps, like this:
This is the standard usage of
dv.table
(array of maps, which is the same as an array of pages since pages are maps). If you have a map of maps as your last example, you'll need to go through one more level of mapping: