danww
1
I’m a bit stuck.
%{"TopKey" => [
%{"StringKey" => "foo"},
%{"StringKey" => "bar"}
]
}
I have a map, containing a list of maps, as above.
I’m struggling to find the correct combination of list/map operations to perform an in-situ string operation on the value of ‘StringKey’.
Most examples of manipulation of nested structures deal solely with maps or lists, not combinations thereof.
1 Like
my_data = %{"TopKey" => [
%{"StringKey" => "foo"},
%{"StringKey" => "bar"}
]
}
all = fn :get_and_update, data, next ->
Enum.map(data, next) |> :lists.unzip
end
IO.inspect get_and_update_in(my_data, ["TopKey", all, "StringKey"], &{&1, String.upcase &1})
{["foo", "bar"], %{"TopKey" => [%{"StringKey" => "FOO"}, %{"StringKey" => "BAR"}]}}
From Kernel.get_and_update_in/3
my_data = %{"TopKey" => [
%{"StringKey" => "foo"},
%{"StringKey" => "bar"}
]
}
IO.inspect (update_in my_data, ["TopKey", Access.all(), "StringKey"], &String.upcase/1)
%{"TopKey" => [%{"StringKey" => "FOO"}, %{"StringKey" => "BAR"}]}
From Access behaviour
7 Likes
danww
3
Thank you!
I’d found Kernel.update_in
, but the Access.all()
was what I was missing.
Cheers!
1 Like