How to iterate and look conditionally over the list of maps

I want to know if is_selected is set true among any of the map those are inside list then return true else false. How can I do that? Your help will be appreciated
Following is my list of maps

scq_answer: [
    %Apollo.Applicant.Schema.SCQAnswer{
      __meta__: #Ecto.Schema.Metadata<:loaded, "public", "scq_answers">,
      attempt: #Ecto.Association.NotLoaded<association :attempt is not loaded>,
      attempt_id: 223,
      choice: "Samsung",
      id: 432,
      inserted_at: ~N[2019-06-14 06:42:41],
      is_correct: false,
      is_selected: true,
      updated_at: ~N[2019-06-14 06:42:59]
    },
    %Apollo.Applicant.Schema.SCQAnswer{
      __meta__: #Ecto.Schema.Metadata<:loaded, "public", "scq_answers">,
      attempt: #Ecto.Association.NotLoaded<association :attempt is not loaded>,
      attempt_id: 223,
      choice: "Apple",
      id: 433,
      inserted_at: ~N[2019-06-14 06:42:41],
      is_correct: false,
      is_selected: false,
      updated_at: ~N[2019-06-14 06:42:41]
    },
    %Apollo.Applicant.Schema.SCQAnswer{
      __meta__: #Ecto.Schema.Metadata<:loaded, "public", "scq_answers">,
      attempt: #Ecto.Association.NotLoaded<association :attempt is not loaded>,
      attempt_id: 223,
      choice: "Huwaei",
      id: 434,
      inserted_at: ~N[2019-06-14 06:42:41],
      is_correct: false,
      is_selected: false,
      updated_at: ~N[2019-06-14 06:42:41]
    },
    %Apollo.Applicant.Schema.SCQAnswer{
      __meta__: #Ecto.Schema.Metadata<:loaded, "public", "scq_answers">,
      attempt: #Ecto.Association.NotLoaded<association :attempt is not loaded>,
      attempt_id: 223,
      choice: "Google",
      id: 435,
      inserted_at: ~N[2019-06-14 06:42:41],
      is_correct: true,
      is_selected: false,
      updated_at: ~N[2019-06-14 06:42:41]
    }
  ]

:wave:

Maybe

Enum.any?(answers, fn answer -> answer.is_selected end)

Example

iex(1)> answers1 = [%{is_selected: false},%{is_selected: true}, %{is_selected: false}]
iex(2)> answers2 = [%{is_selected: false},%{is_selected: false}, %{is_selected: false}]
iex(3)> Enum.any?(answers1, fn answer -> answer.is_selected end)
true
iex(4)> Enum.any?(answers2, fn answer -> answer.is_selected end)
false

Shortened version:

Enum.any?(answers, & &1.is_selected)
5 Likes