Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 从哈希数组中选择一个值_Arrays_Ruby_Hash - Fatal编程技术网

Arrays 从哈希数组中选择一个值

Arrays 从哈希数组中选择一个值,arrays,ruby,hash,Arrays,Ruby,Hash,从哈希数组中: response = [ {"label"=>"cat", "name"=>"kitty", "id"=>189955}, {"label" => "dog", "name"=>"rex", "id" => 550081} ] 有没有一种方法可以写出这样的内容: response.name.kitty 要检索包含此值的哈希,请执行以下操作: {"label"=>"cat", "name"=>"kitty", "id"=&

从哈希数组中:

response = [
  {"label"=>"cat", "name"=>"kitty", "id"=>189955},
  {"label" => "dog", "name"=>"rex", "id" => 550081}
]
有没有一种方法可以写出这样的内容:

response.name.kitty
要检索包含此值的哈希,请执行以下操作:

{"label"=>"cat", "name"=>"kitty", "id"=>189955}
你可以这样做-

response.select{|x| x["name"] == "kitty"}.first
使用进程

response = [{"label"=>"cat", "name"=>"kitty", "id"=>189955},
            {"label" => "dog", "name"=>"rex", "id" => 550081}]
finder = Proc.new {|k, v| response.find {|r| r[k] == v} }
然后


如果需要,您还可以传递多个条件,如:

response = [
  {"label"=>"cat", "name"=>"kitty", "id"=>189955},
  {"label"=>"cat", "name"=>"kitty", "id"=>189956},
  {"label"=>"cat", "name"=>"kitty", "id"=>189957},
  {"label"=>"cat", "name"=>"meow", "id"=>189957},
  {"label" => "dog", "name"=>"rex", "id" => 550081},
  {"label" => "dog", "name"=>"tommy", "id" => 550082},
  {"label" => "dog", "name"=>"rex", "id" => 550083}
]

> response.select{|h| h["label"] == "cat" && h["name"] == "kitty" && h["id"] == 189955}
=> [{"label"=>"cat", "name"=>"kitty", "id"=>189955}] # returns array of selected hashes

> response.find{|h| h["label"] == "cat" && h["name"] == "kitty" && h["id"] == 189955}
=> {"label"=>"cat", "name"=>"kitty", "id"=>189955} # returns first match element

如果您使用的是Rails,只需使用
.pull

array_of_hashes = [
  { name: "Kate", city: "Minsk", country: "Belarus", id: 1 },
  { name: "Mike", city: "New York", country: "USA", id: 2 },
  { name: "Aleh", city: "Warsaw", country: "Poland", id: 3},
]


>> array_of_hashes.pluck(:name)
>>
>> [ "Kate", "Mike", "Aleh" ]

在Ruby中,有一种方法可以通过重写缺少的方法来动态地执行任何事情。你的想法的目的是什么?@dcorking:也许会把同事搞糊涂吧?:)我的想法是,在ruby中可能有一种方法,通过使用Struts或OpenStruts进行元编程,允许您从哈希数组或深度嵌套哈希数组中动态选择值。也许我需要将其转换为json,然后使用实例方法。对不起,如果我没有清楚地解释我想要实现的目标,请解释为什么Amit的答案不能满足您的需要。这可能有助于我们更好地理解您的问题。Amit的答案很好,我正在寻找的是ruby中可能有一种方法可以在哈希数组中动态选择值。这会给您以下结果:
{“label”=>“cat”,“name”=>“kitty”,“id”=>189955}
。示例:或者只使用detect/find而不是select+first@SergioTulentsev......yes,我们可以这样做>>
response.find{x | x[“name”]=“kitty”}
我喜欢这种方式
Proc
1+用于此或
finder['name',rex']
通过一些调整,程序可以接受散列,即
finder['label'=>cat',name'=>sam]
选择
+
第一个
查找所有元素并丢弃除第一个之外的所有元素。使用
find
,它会在第一次匹配后停止。
array_of_hashes = [
  { name: "Kate", city: "Minsk", country: "Belarus", id: 1 },
  { name: "Mike", city: "New York", country: "USA", id: 2 },
  { name: "Aleh", city: "Warsaw", country: "Poland", id: 3},
]


>> array_of_hashes.pluck(:name)
>>
>> [ "Kate", "Mike", "Aleh" ]