Prepending a value to a DataviewJS array

I’m using Templater to walk someone through the creation of an “entity” in my knowledge base, whether it’s a person, client, or other company. To do that, I’m using Dataview here and there so that for “company” entities, I can link directly to a “person” page (using dv.pagePaths to offer note links to pick via tp.system.suggester)

However, I’m planning for the case where the desired person’s note is not yet created. I’d like to prepend an option to the array called “New Entry”, where if it’s selected, I can then run another prompt via an if statement.

The problem I’m running into is that I can’t figure out if there’s a command for prepending an array inside of DataviewJS. I’ve tried entityArray.prepend and entityArray.unshift, but neither seem to be supported. The error I get in the console is entityArray.prepend is not a function.

I’m aware that there’s a way to convert to a Javascript array and back, but I’d like to not add even more complexity if I can help it.

If you want the full applicable code, look below.

<%*
//"Loads" DataviewJS
const dv = app.plugins.plugins.dataview.api

    //Loads list of potential "people" to slot in as a contact
	var entityArray = await dv.pagePaths('"People, Companies, Clients"', "#person")
	var newArray = entityArray.prepend("New Entry")

	// Asks for the name of the contact for the company
	var prelimOutput = await tp.system.suggester(newArray, newArray, true, "Who is the contact for " + title + "? (if your contact isn't listed, select \"New Entry\")")
	
	// To create a prompt if the contact name isn't in the system yet
	if (prelimOutput == "New Entry") {
		entityOutput = await tp.system.prompt("What's the name of the contact for " + title + "?")
	} else {
		entityOutput = "contact: \"[[" + prelimOutput + "]]\""
	}
%>

So yeah, any easy way to prepend to a DataviewJS array?

Someone in the Discord brought up using concat(), so thanks to them for sparking this solution:

<%*
//"Loads" DataviewJS
const dv = app.plugins.plugins.dataview.api

    //Loads list of potential "people" to slot in as a contact, adding "New Entry" as an option
	var entityArray = await dv.pagePaths('"People, Companies, Clients"', "#person")
	var newArray = dv.array(["New Entry"])
        newArray = newArray.concat(entityArray)

	// Asks for the name of the contact for the company
	var prelimOutput = await tp.system.suggester(newArray, newArray, true, "Who is the contact for " + "this client" + "? (if your contact isn't listed, select \"New Entry\")")
	
	// To create a prompt if the contact name isn't in the system yet
	if (prelimOutput == "New Entry") {
		entityOutput = await tp.system.prompt("What's the name of the contact for " + "this client" + "?")
	} else {
		entityOutput = "contact: \"[[" + prelimOutput + "]]\""
	}
%>

I now feel all-powerful! Thanks, Dataview and Templater! :+1:t2: Now, to figure out how to write a “prepend” function on my own, using this great knowledge…

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