# Extract a field from an array of structures

**URL:** https://discourse.julialang.org/t/extract-a-field-from-an-array-of-structures/82916
**Category:** General Usage
**Created:** [June 17, 2022, 1:07pm UTC](https://discourse.julialang.org/t/extract-a-field-from-an-array-of-structures/82916 "2022-06-17T13:07:15Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)
#### Post date: [June 17, 2022, 1:07pm UTC](https://discourse.julialang.org/t/extract-a-field-from-an-array-of-structures/82916/1 "2022-06-17T13:07:15Z")

</div>

I was wondering if there is a more elegent way to extract a field from an array of structures. An example follows:

```julia

julia> struct Foo
           i::Int
           v::Vector{Int}
       end

julia> foo = Vector{Foo}(undef, 2)
2-element Vector{Foo}:
 #undef
 #undef

julia> foo[1] = Foo(1, [1, 2])
Foo(1, [1, 2])

julia> foo[2] = Foo(3, [6, 8])
Foo(3, [6, 8])

julia> foo
2-element Vector{Foo}:
 Foo(1, [1, 2])
 Foo(3, [6, 8])

julia> # obtain the first field of the vector of structures

julia> [foo[a].i for a in 1:length(foo)]
2-element Vector{Int64}:
 1
 3

julia>

```

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [June 17, 2022, 2:05pm UTC](https://discourse.julialang.org/t/extract-a-field-from-an-array-of-structures/82916/2 "2022-06-17T14:05:09Z")

</div>

There is [GitHub - JuliaArrays/StructArrays.jl: Efficient implementation of struct arrays in Julia](https://github.com/JuliaArrays/StructArrays.jl)

---

<div class="post-metadata">

### Author: ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)
#### Post date: [June 17, 2022, 2:06pm UTC](https://discourse.julialang.org/t/extract-a-field-from-an-array-of-structures/82916/3 "2022-06-17T14:06:37Z")

</div>

You can use broadcasting with either the `getproperty` or `getfield` functions:

```julia
julia> getproperty.(foo, :i)
2-element Vector{Int64}:
 1
 3

julia> getfield.(foo, :i)
2-element Vector{Int64}:
 1
 3

```

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [June 17, 2022, 2:36pm UTC](https://discourse.julialang.org/t/extract-a-field-from-an-array-of-structures/82916/4 "2022-06-17T14:36:49Z")

</div>

> [@Jake](#):
>
> `julia> [foo[a].i for a in 1:length(foo)]`

If you want to do it with a comprehension, this is shorter and better:

```julia
[x.i for x in foo]

```

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 17, 2022, 6:45pm UTC](https://discourse.julialang.org/t/extract-a-field-from-an-array-of-structures/82916/5 "2022-06-17T18:45:07Z")

</div>

Shorter it is, _better_ is debatable. The broadcast solution is probably better situations in you want dot fusion to happen, and the `StructArrays` is a more general solution.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [June 17, 2022, 7:25pm UTC](https://discourse.julialang.org/t/extract-a-field-from-an-array-of-structures/82916/6 "2022-06-17T19:25:00Z")

</div>

It absolutely _is_ better than the indexing version. For one thing it doesn’t assume anything about the axes, and it does natural iteration.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [June 17, 2022, 9:09pm UTC](https://discourse.julialang.org/t/extract-a-field-from-an-array-of-structures/82916/7 "2022-06-17T21:09:40Z")

</div>

> [@Henrique\_Becker](#):
>
> Shorter it is, _better_ is debatable. The broadcast solution is probably better situations in you want dot fusion to happen, and the `StructArrays` is a more general solution.

English isn’t my first language but I thought I made it clear that I compared my proposal to the quoted comprehension approach. I had no intention to claim anything about its relative merits to other solutions.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 17, 2022, 11:50pm UTC](https://discourse.julialang.org/t/extract-a-field-from-an-array-of-structures/82916/8 "2022-06-17T23:50:03Z")

</div>

I apologize, I must have read it fast. In retrospective, it is obvious that you are improving just on the previous comprehension solution. What seems to have caused my confusion is that I did not scroll down all of Jake’s code. So when I saw you were referring one previous alternative I thought you could only be referring to the broadcast or the package.
