Accessing outer AND inner indexes inside nested arrays at the same time - dust.js

I've got nested arrays such as:
"outer" : [
{
"inner": [
{},
{}
]
},
{
"inner": [
{},
{}
]
}
]
I neeed to generate an output like:
outer[0].inner[0]
outer[0].inner[1]
outer[1].inner[0]
outer[1].inner[1]
My problem is that once I'm inside the inner context, I do not know the outer index. $idx gives me the index of inner. Is there a way for me to access the outer index inside the inner context?
Template example:
{#outer}
{#inner}
outer[???].inner[{$idx}]
{/inner}
{/outer}

In this case you do not need to know the outer context, you could just pass the outer index($idx) as a parameter.
{#outer}
{#inner outIdx=$idx}
outer[{outIdx}].inner[{$idx}]
{/inner}
{/outer}

Related

iOS Swit 3 - filter array inside filter

I would like to filter array inside a filter. First I have a big array of Staff object (self.bookingSettings.staffs). Inside this array I have multiple object like this :
"staffs": [
{
"id": 1,
"name": "Brian",
"services": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
},
{
"id": 4
}
],
"pos": 1
},...
I would like to filter this array in order to have only services with id = 3.
I succeed to have if first object is equal to 3 with this code :
self.bookingSettings.staffs.filter({ $0.services.first?.id == self.bookingService.id })
but that takes only the first item.
I think I have to filter inside my filter function, something like this to loop over all object inside services :
self.bookingSettings.staffs.filter({ $0.services.filter({ $0.id == self.bookingService.id }) })
but I've the following error: Cannot convert value of type [BookingService] to closure result type Bool.
Is this a good idea ? How can I achieve this ?
You could use filter, which would look something like this:
self.bookingSettings.staffs.filter {
!$0.services.filter{ $0.id == self.bookingService.id }.isEmpty
}
This code is constructing an entire array of filtered results, only to check if its empty and immediately discard it. Since filter returns all items that match the predicate from the list, it won't stop after it finds a match (which is really what you're looking for). So even if the first element out of a list of a million elements matches, it'll still go on to check 999,999 more elements. If the other 999,999 elements also match, then they will all be copied into filter's result. That's silly, and can use way more CPU and RAM than necessary in this case.
You just need contains(where:):
self.bookingSettings.staffs.filter {
$0.services.contains(where: { $0.id == self.bookingService.id })
}
contains(where:) short-circuits, meaning that it won't keep checking elements after a match is found. It stops and returns true as soon as find a match. It also doesn't both copying matching elements into a new list.

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

Modifying NSMutableDictionary by key path

I have a NSDictionary with nested NSDictionary/NSArray (Generated through a JSON).
I need to remove objects in multiple places according to some logic imposed on me.
For instance lets say that the dictionary has some structure like:
{
"Array1" : [
{
"InnerArray1" : [
{
"some conditional field" : Evaluate this ...
}
],
"More Properties : ....
}
],
"Array2" : {
... some complex inner structure
{
// some inner object to remove according to "evaluate condition"
}
}
}
When I iterate the "Array1.InnerArray1..." and evaluate some condition to true , I need to remove objects in a disjoint location.
Ideally I could do something like
foreach item in Array1.InnerArray1 {
if item evaluates to true {
// evaluate the disjoint location...
remove Array2.....InnerObject(s) where ConditionField == true
}
}
Currently it is very awkward to iterate the inner objects and mutate them on the same pass (and very hard to read).
Given I have created a deep mutable copy of this whole data tree, is it possible to remove nested objects using kvc or another mechanism without nested iterations?

Groovy/Grails : How to groupBy multiple keys combined?

I have a list and i want to group by all three keys, i refer to How to group a list of list.
def given = [
[Country:'Japan',Flag:'Yes',Event:'New Year'],
[Country:'china',Flag:'No',Event:'Spring Festival'],
[Country:'uk',Flag:'No',Event:'National Holiday'],
[Country:'us',Flag:'Yes',Event:'Labour Day'],
[Country:'us',Flag:'Yes',Event:'New Year'],
[Country:'uk',Flag:'Yes',Event:'Memorial Day']
]
We can group by:
def mapped = given.groupBy {
[(it["Country"]) : it["Flag"] ] }
How can I group by [(it["Country"]) : it["Flag"] : it["Event"] ] ?
expected results : [['Japan':['Yes':[NewYear]]]:[['Country':'Japan', 'Flag':'Yes', 'Event':'New Year']] , ..
What this is good for, I don't understand. #dmahapatro 's solution gives a much more handlable result. In your example you just want to have a recursive map as key for the group by. I have my strongest doubts, that this will handle the actual grouping case well.
def given = [
[Country:'Japan',Flag:'Yes',Event:'New Year'],
[Country:'china',Flag:'No',Event:'Spring Festival'],
[Country:'uk',Flag:'No',Event:'National Holiday'],
[Country:'us',Flag:'Yes',Event:'Labour Day'],
[Country:'us',Flag:'Yes',Event:'New Year'],
[Country:'uk',Flag:'Yes',Event:'Memorial Day']
]
println given.groupBy{ [(it.Country): [(it.Flag): [it.Event]]] }.inspect()
//=> [['Japan':['Yes':['New Year']]]:[['Country':'Japan', 'Flag':'Yes', 'Event':'New Year']], ...
given.groupBy( { it.Country }, { it.Flag }, { it.Event } )
A method taking 3 closures as arguments.

Resources