1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-07 09:08:01 +00:00

Add All Values For A Key In A Keyword List as an elixir til

This commit is contained in:
jbranchaud
2019-02-27 18:12:31 -06:00
parent 12e0c8fc6c
commit be0a33d861
2 changed files with 24 additions and 1 deletions

View File

@@ -0,0 +1,22 @@
# All Values For A Key In A Keyword List
A keyword list in Elixir can contain the same key multiple times.
```elixir
kwl = [a: 1, b: 2, a: 3, c: 4]
#=> [a: 1, b: 2, a: 3, c: 4]
```
The `get/2` function will only grab the value of the first occurrence.
```elixir
Keyword.get(kwl, :a)
#=> 1
```
You can use `get_values/2` to retrieve _all_ values associated with the key.
```elixir
Keyword.get_values(kwl, :a)
#=> [1, 3]
```