Skip to main content Link Search Menu Expand Document Toggle dark mode Copy Code (external link)
paperwork.help

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 any IDictionary or 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 JObject or System.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 null value, 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

<p>{{join(keys(model.prices), ', ')}}</p>

Output:

<p>Widget, Gizmo, Gadget</p>

Sort the keys

Sort the keys themselves with sortBy(keys(...)), and add 'desc' for descending order.

{{#each sortBy(keys(model.prices))}}
  <p>{{this}}: {{model.prices[this]}}</p>
{{/each}}

Output:

<p>Gadget: 14.00</p>
<p>Gizmo: 3.25</p>
<p>Widget: 9.50</p>

Check the number of entries

{{#if count(keys(model.prices)) > 2}}
  <p>{{count(keys(model.prices))}} prices</p>
{{else}}
  <p>Not enough prices</p>
{{/if}}

Property names of an object

<p>{{join(keys(model.person), ', ')}}</p>

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. Use keys() when the keys need to be sorted, counted or tested first.
  • Takes exactly one parameter.
  • Added in Paperwork.Core 9.8.

See Also