Does Dust.js provide a way to reference an object key/value by keywords "key" and "value"? -
i want use dust.js client template engine. have data json this:
var data = { "foo": [{ "somekey": "somevalue", "otherkey": "othervalue" }, { "somekey": "somevalue", "otherkey": "othervalue" }], "bar": [{ "somekey": "somevalue", "otherkey": "othervalue" }, { "somekey": "somevalue", "otherkey": "othervalue" }] }
i not know in advance uppermost object keys - not know foo
, bar
keys, can value.
so, need iterate through json keywords key
, value
. in pseudo-code:
{% for(key, value) in data %} {key}: {value} {% /for %}
i know dust.js has {#section/}
loop through object. again, have provide key name:
{#extradata} {! inside section, dust looks values within extradata object !} inside section, value of name is: {name}{~n} {/extradata}
and not know extradata
name in advance.
so, dust.js provide way reference object keys/values key
, value
keywords?
dust not provide built-in iteration on objects.
however, can add {@iterate}
helper type of iteration.
you can @ https://www.npmjs.com/package/dustmotes-iterate
example usage:
data: { obj: {a:"a", b:"b", c:"c" } } {@iterate key=obj} {$key}:{$value} {$type} {/iterate} output: a:a string b:b string c:c string