# How to simply get the array struct's element array

**URL:** <https://discourse.julialang.org/t/how-to-simply-get-the-array-structs-element-array/82965>\
**Category:** General Usage\
**Tags:** struct\
**Created:** [June 18, 2022, 12:07pm UTC](https://discourse.julialang.org/t/how-to-simply-get-the-array-structs-element-array/82965 "2022-06-18T12:07:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cepheid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cepheid/32/24934_2.png) [@cepheid](https://discourse.julialang.org/u/cepheid)\
**Post date:** [June 18, 2022, 12:07pm UTC](https://discourse.julialang.org/t/how-to-simply-get-the-array-structs-element-array/82965/1 "2022-06-18T12:07:46Z")

</div>

For example:

```julia
mutable struct Coords
           x::Float64
           y::Float64
           z::Float64
 end
n = 10
v = Vector{Coords}(undef, n)
for i = 1:n
      v[i] = Coords(rand(), rand(), rand())
end

```

I want to get array struct v’s all element x values as vector,

```julia
julia> v[:].x
ERROR: type Array has no field x
Stacktrace:
 [1] getproperty(x::Vector{Coords}, f::Symbol)
   @ Base ./Base.jl:42
 [2] top-level scope
   @ REPL[11]:1

```

```julia
julia> [v[i].x for i=1:10]

```

This can get the result I want, but the expression is not elegant.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 18, 2022, 12:42pm UTC](https://discourse.julialang.org/t/how-to-simply-get-the-array-structs-element-array/82965/2 "2022-06-18T12:42:58Z")

</div>

`getproperty.(v, :x)`

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [June 18, 2022, 12:48pm UTC](https://discourse.julialang.org/t/how-to-simply-get-the-array-structs-element-array/82965/3 "2022-06-18T12:48:05Z")

</div>

Or you use a StructArray:

```julia
using StructArrays

mutable struct Coords
    x::Float64
    y::Float64
    z::Float64
end

n = 10
v = Vector{Coords}(undef, n)
for i = 1:n
    v[i] = Coords(rand(), rand(), rand())
end

sa = StructArray(v)
sa.x

```

@cepheid Please put your example code in triple backticks (```) so that it gets nicely formatted.
