Does Dust.js provide a way to reference an object key/value by keywords "key" and "value"? - dust.js

I want to use Dust.js as a client template engine.
I have a data json like this:
var data = {
"Foo": [{
"somekey": "somevalue",
"otherkey": "othervalue"
}, {
"somekey": "somevalue",
"otherkey": "othervalue"
}],
"Bar": [{
"somekey": "somevalue",
"otherkey": "othervalue"
}, {
"somekey": "somevalue",
"otherkey": "othervalue"
}]
}
I do not know in advance what uppermost object keys will be - I do not know Foo and Bar keys, they can be any value.
So, I need to iterate through this json by keywords like key and value. Something like in this pseudo-code:
{% for(key, value) in data %}
{key}: {value}
{% /for %}
I know that Dust.js has {#section/} to loop through an object. But again, you have to provide a key name:
{#extraData}
{!
Inside this section, Dust looks for
values within the extraData object
!}
Inside the section, the value of name is: {name}{~n}
{/extraData}
And I do not know extraData name in advance.
So, does Dust.js provide a way to reference object keys/values by key and value keywords?

Dust does not provide built-in iteration over objects.
However, you can add the {#iterate} helper to do this type of iteration.
You can get it at 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

Related

Elasticsearch saves document as string of array, not array of strings

I am trying to contain array as a document value.
I succeed it in "tags" field as below;
This document contains array of strings.
curl -XGET localhost:9200/MY_INDEX/_doc/132328908
#=> {
"_index":"MY_INDEX",
"_type":"_doc",
"_id":"132328908",
"found":true,
"_source": {
"tags": ["food"]
}
}
However, when I am putting items in the same way as above,
the document is SOMETIMES like that;
curl -XGET localhost:9200/MY_INDEX/_doc/328098989
#=> {
"_index":"MY_INDEX",
"_type":"_doc",
"_id":"328098989",
"found":true,
"_source": {
"tags": "[\"food\"]"
}
}
This is string of array, not array of strings, which I expected.
"tags": "[\"food\"]"
It seems that this situation happens randomly and I could not predict it.
How could it happen?
Note:
・I use elasticsearch-ruby client to index a document.
This is my actual code;
es_client = Elasticsearch::Client.new url: MY_ENDPOINT
es_client.index(
index: MY_INDEX,
id: random_id, # defined elsewhere
body: {
doc: {
"tags": ["food"]
},
}
)
Thank you in advance.

RoR - Find if part of a string matches an array of hashes

I know there are a lot of similar questions but I am struggling to find a specific answer.
i have an array of hashes with key of Symbol and a value of Price, I am looking to filter the array to only include the hashes that have a Symbol that ends in the letters ETL
Data looks like:
[ [
{
"symbol": "ABCDEF",
"price": "4"
},
{
"symbol": "GHIETL",
"price": "5"
}
]
You can use something like this:
array.select { |hash| hash[:symbol].end_with? "ETL" }
From the Ruby docs for select:
Returns an array containing all elements of enum for which the given block returns a true value.
You can also provide multiple suffixes to end_with? if you need to filter by multiple suffixes. For example:
array.select { |hash| hash[:symbol].end_with? "ETL", "DEF" }

Rails PSQL query JSON for nested array and objects

So I have a json (in text field) and I'm using postgresql and I need to query the field but it's nested a bit deep. Here's the format:
[
{
"name":"First Things",
"items":[
{
"name":"Foo Bar Item 1",
"price":"10.00"
},
{
"name":"Foo Item 2",
"price":"20.00"
}
]
},
{
"name":"Second Things",
"items": [
{
"name":"Bar Item 3",
"price":"15.00"
}
]
}
]
And I need to query the name INSIDE the items node. I have tried some queries but to no avail, like:
.where('this_json::JSON #> [{"items": [{"name": ?}]}]', "%#{name}%"). How should I go about here?
I can query normal JSON format like this_json::JSON -> 'key' = ? but need help with this bit.
Here you need to use json_array_elements() twice, as your top level document contains array of json, than items key has array of sub documents. Sample psql query may be the following:
SELECT
item->>'name' AS item_name,
item->>'price' AS item_price
FROM t,
json_array_elements(t.v) js_val,
json_array_elements(js_val->'items') item;
where t - is the name of your table, v - name of your JSON column.

How to declare an object with un-named primitives in swagger?

I need to represent the following array in swagger-2.0, but am unsure how to do it, since I cannot figure out how to declare 'un-named' properties.
For example, here's what I need to define:
coords: [
[
37.782984,
-122.420973
],
[
37.772309,
-122.418555
],
...
]
How can I define these array-entries in swagger ?
If you model it as an array of arrays with two items, it should look like this:
parameters:
- name: coords
in: body
schema:
type: array
items:
type: array
items:
type: number
maxItems: 2
minItems: 2

How to get other documents' data by document id in map function

I have a problem "joining" some documents in a view. Here's my schema: Documents with type "category" can hold an embedded array of ids to documents with type "page". Both have a field "name".
My Documents:
{
"_id": "887c28dcf6dd8429d404d276b900b203",
"_rev": "3-c4d1e0c8378bb0081b5fe3522ee649a0",
"TYPE": "category",
"NAME": "Home",
"PAGES": [
{"PAGE_ID": "887c28dcf6dd8429d404d276b900ad8f"},
{"PAGE_ID": "887c28dcf6dd8429d404d276b900b203"},
{"PAGE_ID": "887c28dcf6dd8429d404d276b900bae0"}
]
}
{
"_id": "887c28dcf6dd8429d404d276b9008c01",
"_rev": "1-afd54c654ae5afa56a3fbd7b1ba119d2",
"TYPE": "page",
"NAME": "Foo"
}
Now I want to join these in a view with this map function:
function(doc) {
if (doc.TYPE == "category") {
for (id in doc.PAGES) {
emit(doc.NAME, doc.PAGES[id].PAGE_ID);
}
}
}
But instead of the PAGE_ID I want the NAME of the referencing document.
This is basically the example from the CouchDB wiki, but they don't show the map function. So any ideas?
You need to include a _id field somewhere in your output value. After that, you use include_docs=true and the view will include the linked document instead of the source document. For example:
function(doc) {
if (doc.TYPE == "category") {
for (var x = 0; x < doc.PAGES.length; x++) {
emit(doc.NAME, { _id: doc.PAGES[x].PAGE_ID });
}
}
}
When you query this view, you add include_docs=true to your URL. And your view output will add a new field to each row called doc. Ordinarily, that field will contain the full source document. In this case, it will include the linked document you are referring to.
EDIT Btw, you're using the for...in loop incorrectly. A for...in is used to loop over the properties of an object. You need to use a simple for loop to iterate an array like this.

Resources