Range first where - on nested data

Good to know it works for you.

In my code above, I set {{ $count := 10 }} near the top, and then use it as a decreasing counter inside the if block:
{{ $count = sub $count 1 }}

You can set the initial value of $count to how ever many items you want to iterate through. This replaces the need to use first 10 in the code.

Let me explain the reason first 10 does not work.

Using your original code:


<!-- $feed is an element of .Site.Data.mydata which is an array -->

{{ range $feed := .Site.Data.mydata }}                    <!-- 1 -->
  {{ range ( where $feed.PARAM "VAL" "eq" "camel" ) }}    <!-- 2 -->
    {{ if in $feed.CATEGORYTEXT "crossbody" }}            <!-- 3 -->
    <li>{{ $feed.PRODUCT }}</li>                          <!-- 4 -->
    {{ end }}                                             <!-- 5 -->
  {{ end }}                                               <!-- 6 -->
{{ end }}                                                 <!-- 7 -->

If you call range first 10 ... from line 2, your context is $feed.

<!-- truncated version of what $feed looks like -->
{
  "ITEM_ID": ...,
  "PARAM": [{
      "PARAM_NAME": "...",
      "VAL": "..."
    },
  ],
  "CATEGORYTEXT": "...",
  ...
}

$feed itself is a map though, and from the docs on first collections.First | Hugo (emphasis mine):

Slices an array to only the first N elements.

which explains why it does not work on maps.

To illustrate, with javascript for example, it is the equivalent of doing something like the following:

var feed = {
  "ITEM_ID" : value1,
  "PARAM" : [{"PARAM_NAME": "p1", "VAL": "v1"},{"PARAM_NAME": "p2", "VAL": "v2"}  ] ,
  "CATEGORYTEXT" : value3,
  "key4" : value4,
  "key5" : value5
}
for (var i = 0; i < 3; i++) {
  console.log(feed[i])
}

… which just does not make sense.





You can’t use first 10 from the outermost range either, because it counts each element in .Site.Data.mydata even if it does not satisfy the inner conditions.

I hope I was able to explain things well enough.