Dataview JS table from arrays declared in code block

Let’s suppose I’m using the following sample data in the form of JS arrays declared within a DataviewJS code block:

//fruits I have
let arr1 = ['oranges', 'bananas', 'pineapples']

//number of each fruit on hand
let arr2 = [3, 0, 5]

The “why” of the situation is complex, but hopefully it’s enough to know that these are the data I’m working with. Two arrays, arr1 and arr2, for which the indices line up (i.e., arr1[0] corresponds to arr2[0]).

I need to make a two-column table (in this example, the column headings would be “Fruit” and “# on hand”. What’s the best way to accomplish this using DataviewJS?

Here’s what I came up with based on your description (make sure to include that in a dataviewjs codeblock):

dv.table(
	["Fruit", "\\# on hand"], 
	arr1.map((fruit, index) => [fruit, arr2[index]])
);
Fruit # on hand
oranges 3
bananas 0
pineapples 5
2 Likes

This did the trick, thank you so much!!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.