Last active
November 28, 2017 07:53
-
-
Save keroro520/61173bda3b1441e8a6be25eb0166c6f8 to your computer and use it in GitHub Desktop.
How can I iterate two list of maps at once?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule IterateProposalsCustomers do | |
def input do | |
proposals = [ | |
%{customer_id: "5202845", other: "foo"}, | |
%{customer_id: "9778978", other: "foo"} | |
] | |
customers = [ | |
%{id: "5202845", name: "foo"}, | |
%{id: "5643635", name: "bar"}, | |
%{id: "9778978", name: "baz"}, | |
%{id: "3423454", name: "boo"} | |
] | |
{proposals, customers} | |
end | |
def run(proposals, customers) do | |
customer_ids = Enum.map(proposals, &Map.get(&1, :customer_id)) | |
customers_id_names = Enum.map(customers, &{Map.get(&1, :id), Map.get(&1, :name)}) |> Enum.into(%{}) | |
Map.take(customers_id_names, customer_ids) | |
end | |
def run do | |
{proposals, customers} = input | |
run(proposals, customers) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment