gpt4 book ai didi

elixir - 获取指定索引对象时出错

转载 作者:行者123 更新时间:2023-12-02 03:15:45 26 4
gpt4 key购买 nike

我想从对象列表中获取第一个索引对象。这是示例对象:

[%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}]

我想获取指定的索引对象。像:

%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}

我尝试使用objects[0]获取对象,但它给出了以下错误消息。错误:** (ArgumentError) Access 调用关键字期望键是一个原子,得到:0

请帮我解决这个问题。

最佳答案

您有几个选项可以从列表中获取元素。

如果您想要第一项(索引 0),您可以这样做:

[item | _tail] = items

列表头部的条目将绑定(bind)到item

您还可以使用hd/1在管道中有用的函数:

item = hd(items)

如果您想要列表中的特定索引,可以使用 Enum.at/3 :

item = Enum.at(items, 5)

关于elixir - 获取指定索引对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36280176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com