azh*_*ik3 2 elixir phoenix-framework
我想从对象列表中获取第一个索引对象.这是示例对象:
[%VendorModel{__meta__: #Ecto.Schema.Metadata<:loaded>,
cameras: #Ecto.Association.NotLoaded<association :cameras is not loaded>,
config: %{"auth" => %{"basic" => %{"password" => "12345",
"username" => "admin"}},
"snapshots" => %{"h264" => "h264/ch1/main/av_stream",
"jpg" => "Streaming/Channels/1/picture"}}, exid: "ds-2cd2032-i", id: 332,
name: "DS-2CD2032-I",
vendor: #Ecto.Association.NotLoaded<association :vendor is not loaded>,
vendor_id: 6},
%VendorModel{__meta__: #Ecto.Schema.Metadata<:loaded>,
cameras: #Ecto.Association.NotLoaded<association :cameras is not loaded>,
config: %{"auth" => %{"basic" => %{"password" => "12345",
"username" => "admin"}},
"snapshots" => %{"h264" => "h264/ch1/main/av_stream",
"jpg" => "Streaming/Channels/1/picture", "mjpg" => "/",
"mpeg4" => "mpeg4/ch1/main/av_stream"}}, exid: "ds-2cd2612f-i", id: 2911,
name: "DS-2CD2612F-I",
vendor: #Ecto.Association.NotLoaded<association :vendor is not loaded>,
vendor_id: 6},
%VendorModel{__meta__: #Ecto.Schema.Metadata<:loaded>,
cameras: #Ecto.Association.NotLoaded<association :cameras is not loaded>,
config: %{"auth" => %{"basic" => %{"password" => "mehcam",
"username" => "admin"}},
"snapshots" => %{"h264" => "h264/ch1/main/av_stream",
"jpg" => "Streaming/Channels/1/picture", "mjpg" => "/",
"mpeg4" => "mpeg4/ch1/main/av_stream"}}, exid: "ds-2df5274-a", id: 2913,
name: "DS-2DF5274-A",
vendor: #Ecto.Association.NotLoaded<association :vendor is not loaded>,
vendor_id: 6}]
Run Code Online (Sandbox Code Playgroud)
我想获得指定的索引对象.喜欢:
%VendorModel{__meta__: #Ecto.Schema.Metadata<:loaded>,
cameras: #Ecto.Association.NotLoaded<association :cameras is not loaded>,
config: %{"auth" => %{"basic" => %{"password" => "12345",
"username" => "admin"}},
"snapshots" => %{"h264" => "h264/ch1/main/av_stream",
"jpg" => "Streaming/Channels/1/picture"}}, exid: "ds-2cd2032-i", id: 332,
name: "DS-2CD2032-I",
vendor: #Ecto.Association.NotLoaded<association :vendor is not loaded>,
vendor_id: 6}
Run Code Online (Sandbox Code Playgroud)
我试图使用对象,objects[0]但它给出了以下错误消息.错误:** (ArgumentError) the Access calls for keywords expect the key to be an atom, got: 0.
请帮我解决这个问题.
您可以从列表中获取元素.
如果你想要第一个项目(索引0),你可以这样做:
[item | _tail] = items
Run Code Online (Sandbox Code Playgroud)
列表头部的条目将被绑定item.
您还可以使用在管道中有用的hd/1函数:
item = hd(items)
Run Code Online (Sandbox Code Playgroud)
如果您想要列表中的特定索引,可以使用Enum.at/3:
item = Enum.at(items, 5)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2039 次 |
| 最近记录: |