# Find in an array of custom structs if we have an element with a specific field value or not

**URL:** <https://discourse.julialang.org/t/find-in-an-array-of-custom-structs-if-we-have-an-element-with-a-specific-field-value-or-not/76850>\
**Category:** General Usage\
**Tags:** struct\
**Created:** [February 21, 2022, 3:25pm UTC](https://discourse.julialang.org/t/find-in-an-array-of-custom-structs-if-we-have-an-element-with-a-specific-field-value-or-not/76850 "2022-02-21T15:25:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)\
**Post date:** [February 21, 2022, 3:25pm UTC](https://discourse.julialang.org/t/find-in-an-array-of-custom-structs-if-we-have-an-element-with-a-specific-field-value-or-not/76850/1 "2022-02-21T15:25:11Z")

</div>

Dear All,

Is there a neat way to find whether, in an array of custom structs, we have an entry with a specific field value or not (outputs as `true` or `false`)?

For example, consider the following example. I have a custom `struct` defined as follows:

```julia
struct MyStruct
    i::Int64 # corresponds to index i ∈ [-1:3]
    j::Int64 # corresponds to index j ∈ [-1:3]
    k::Int64 # corresponds to index k ∈ [0:3]
end

```

And I have the following array of this custom struct `MyStruct`.

```julia
A = MyStruct[
MyStruct(0, 2, 0), 
MyStruct(2, 0, 0), 
MyStruct(2, 3, 0), 
MyStruct(0, 2, 1), 
MyStruct(2, 0, 1), 
MyStruct(2, 3, 1), 
MyStruct(0, 2, 2), 
MyStruct(2, 0, 2), 
MyStruct(2, 3, 2)]

```

As we can see, for any element of the form `MyStruct(i,j,k)` of `A`, `MyStruct(i,j,k).k` never takes the value `3`, but takes the value `0`,`1`, and `2`. Is there a way to do this programmatically for large arrays of `MyStruct`s, whether in the array of custom structs a specific field value (such as `k == 3`) appears or not (outputs as `true` or `false`) in the entries?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 21, 2022, 3:29pm UTC](https://discourse.julialang.org/t/find-in-an-array-of-custom-structs-if-we-have-an-element-with-a-specific-field-value-or-not/76850/2 "2022-02-21T15:29:31Z")

</div>

`findfirst` returns Nothing if it finds nothing or the first index.  
To find all indices: `findall`  
Example:

```julia
julia> findall(x -> x.k == 2, A)
3-element Vector{Int64}:
 7
 8
 9

```

---

<div class="post-metadata">

**Author:** ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)\
**Post date:** [February 21, 2022, 3:33pm UTC](https://discourse.julialang.org/t/find-in-an-array-of-custom-structs-if-we-have-an-element-with-a-specific-field-value-or-not/76850/3 "2022-02-21T15:33:03Z")

</div>

That was a great solution, thanks @oheil !

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 21, 2022, 10:38pm UTC](https://discourse.julialang.org/t/find-in-an-array-of-custom-structs-if-we-have-an-element-with-a-specific-field-value-or-not/76850/4 "2022-02-21T22:38:25Z")

</div>

An alternative, that outputs the required `true` or `false`:

```julia
any(x -> x.k == 3, A)

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [February 22, 2022, 12:51pm UTC](https://discourse.julialang.org/t/find-in-an-array-of-custom-structs-if-we-have-an-element-with-a-specific-field-value-or-not/76850/5 "2022-02-22T12:51:29Z")

</div>

Nice and fast, here is a similar one too:

```julia
any(s.k==3 for s in A)

```
