Intersect keys in dict with slice

Somewhere in our TOMLs…

network = ["network1", "network3"]

and

[networks.network1]
some = "variables"
[networks.network2]
some = "variables"
[networks.network3]
some = "othervariables"

Is there a way to “prepare” my networks object by removing all table-rows NOT in the network slice?

I thought something like this might do the trick, but have no idea on how to compare the key:

{{ $networks := $networks | intersect (where $key in $network) }}

If needed I could move the network key into the table itself:

[[networks]]
slug = "network1"
some="variables"
[[networks]]
slug = "network2"
some="variables"
[[networks]]
slug = "network3"
some="othervariables"

Right now I am ranging through the networks object and compare the key against the network slice, but that will sort by the networks order and i would want to give the user the option to sort by network config… if that makes sense…

With this:

network = ["network1", "network3"]

[[networks]]
slug = "network1"
some="variables"
[[networks]]
slug = "network2"
some="variables"
[[networks]]
slug = "network3"
some="othervariables"

Do this:

<pre>{{ jsonify (dict "indent" "  ") (where .Params.networks "slug" "in" .Params.network) }}</pre>

To get this:

[
  {
    "slug": "network1",
    "some": "variables"
  },
  {
    "slug": "network3",
    "some": "othervariables"
  }
]
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.