keys() : Keys of a Dictionary or Object
Returns the keys of a dictionary, or the names of the properties of an object, as an array of text values that can be counted, sorted and enumerated.
On this page
Signature
keys(object)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
object |
Dictionary, object or JSON object | Yes | The dictionary or object to get the keys of |
Returns
Type: Array of text
- A
Dictionary<,>(or anyIDictionaryor read-only dictionary) returns its keys, in the order the dictionary enumerates them. Keys that are not text, such as numbers, are converted to text. - A JSON object (Newtonsoft
JObjectorSystem.Text.Json) returns the property names, in the order they are in the JSON. - Any other object returns the names of its public properties.
- A
nullvalue, text, a number, or an array or list has no keys, and returns an empty array.
Examples
Data:
builder.WithData("model", new {
prices = new Dictionary<string, decimal> {
{ "Widget", 9.50m }, { "Gizmo", 3.25m }, { "Gadget", 14.00m }
}
});
List the keys
Output:
<p>Widget, Gizmo, Gadget</p>
Sort the keys
Sort the keys themselves with sortBy(keys(...)), and add 'desc' for descending order.
Output:
<p>Gadget: 14.00</p>
<p>Gizmo: 3.25</p>
<p>Widget: 9.50</p>
Check the number of entries
Property names of an object
With model.person as new { name = "Ann", age = 30 } this outputs name, age.
Notes
- To loop over the entries in their own order, with the value as
this, use{{#each model.prices}}and@key. Usekeys()when the keys need to be sorted, counted or tested first. - Takes exactly one parameter.
- Added in Paperwork.Core 9.8.