Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
Ruby 从数组填充哈希_Ruby_Arrays_Ruby On Rails 3_Ruby On Rails 4_Hash - Fatal编程技术网

Ruby 从数组填充哈希

Ruby 从数组填充哈希,ruby,arrays,ruby-on-rails-3,ruby-on-rails-4,hash,Ruby,Arrays,Ruby On Rails 3,Ruby On Rails 4,Hash,我有这个阵列: params[:types] = [type1, type2, type3...] 我希望使用上述数组以以下方式填充哈希: params[:hash] = { "type1" => { something: something }, "type2" => { something: something

我有这个阵列:

params[:types] = [type1, type2, type3...]
我希望使用上述数组以以下方式填充哈希:

params[:hash] = { 
               "type1" => {
                   something: something
               },
               "type2" => {
                    something: something
               },
           }

对于i…params[:types]中的索引使用类似于
循环的
只需使用数组中的最后一个值填充哈希。

您可以使用
each_with_object
方法执行此操作:

params = {}
params[:types] = ["type1", "type2", "type3"]
params[:types].each_with_object({}) { |k, h| h[k] = { "something" => "something" } }
最后一行将返回:

=> {"type1"=>{"something"=>"something"}, "type2"=>{"something"=>"something"}, "type3"=>{"something"=>"something"}}

您可以使用
each_with_object
方法执行此操作:

params = {}
params[:types] = ["type1", "type2", "type3"]
params[:types].each_with_object({}) { |k, h| h[k] = { "something" => "something" } }
最后一行将返回:

=> {"type1"=>{"something"=>"something"}, "type2"=>{"something"=>"something"}, "type3"=>{"something"=>"something"}}

下面是一个代码片段示例,可以满足您的需要

hash = {}
array.each do |a|                                                
  hash[a.to_s] = { "something" => "something" }                  
end
输出:

hash                                                             
=> {
  "type1" => {
    "something" => "something"
  },
  "type2" => {
    "something" => "something"
  },
  "type3" => {
    "something" => "something"
  }
}

下面是一个代码片段示例,可以满足您的需要

hash = {}
array.each do |a|                                                
  hash[a.to_s] = { "something" => "something" }                  
end
输出:

hash                                                             
=> {
  "type1" => {
    "something" => "something"
  },
  "type2" => {
    "something" => "something"
  },
  "type3" => {
    "something" => "something"
  }
}
您可以这样做:

params = { types: ["type1", "type2", "type3"] }

Hash[params[:types].product([{"something" => "something"}])]
  #=> {"type1"=>{"something"=>"something"},
  #    "type2"=>{"something"=>"something"},
  #    "type3"=>{"something"=>"something"}}
或者使用Ruby 2.1

params[:types].product([{"something" => "something"}]).to_h
如果希望为
参数[:type]
的每个元素使用不同的哈希:

hashes = [{ "something1"=>"something1" }, { "something2"=>"something2" },
          { "something3"=>"something3" }]
然后

您可以这样做:

params = { types: ["type1", "type2", "type3"] }

Hash[params[:types].product([{"something" => "something"}])]
  #=> {"type1"=>{"something"=>"something"},
  #    "type2"=>{"something"=>"something"},
  #    "type3"=>{"something"=>"something"}}
或者使用Ruby 2.1

params[:types].product([{"something" => "something"}]).to_h
如果希望为
参数[:type]
的每个元素使用不同的哈希:

hashes = [{ "something1"=>"something1" }, { "something2"=>"something2" },
          { "something3"=>"something3" }]
然后


您没有尝试过任何东西吗?您没有尝试过任何东西吗?请使用
每个带有\u对象的\u,而不是
注入
。它会产生更干净的代码:
params[:types]。每个带有{u object({})的{u对象({k,h{h[k]={“something”=>“something”}}
,因为不需要在块的末尾返回
哈希值<代码>每个带有对象的对象都知道重用该值。啊,酷!谢谢你的提示,刚刚学到了一些东西!我将更新我的答案。无需添加“编辑:…”注释。在我对您答案的评论和我们查看您答案更改的编辑历史记录的能力之间,我们可以找出所说的内容。(关于堆栈溢出的所有问题和答案都有一个修订历史记录,高于某个声誉的用户可以看到。它有时非常方便。)再次编辑,在这种情况下:)使用
而不是
inject
,使用
每个带有对象的\u
。它会产生更干净的代码:
params[:types]。每个带有{u object({})的{u对象({k,h{h[k]={“something”=>“something”}}
,因为不需要在块的末尾返回
哈希值<代码>每个带有对象的对象都知道重用该值。啊,酷!谢谢你的提示,刚刚学到了一些东西!我将更新我的答案。无需添加“编辑:…”注释。在我对您答案的评论和我们查看您答案更改的编辑历史记录的能力之间,我们可以找出所说的内容。(所有关于堆栈溢出的问题和答案都有一个修订历史记录,高于某个声誉的用户可以看到。它有时非常方便。)再次编辑,在这种情况下:)假设
a.to_s
意味着将变量
type1
的值转换为字符串,那么就没有理由断定这是
“type1”
。我认为,最好是假设OP意味着
参数[:types]=[“type1”、“type2”、“type3”…]
并将
放到
。假设
a.to\u
意味着将变量
type1
的值转换为字符串,那么就没有理由断定这是
“type1”
。我认为,最好是假设OP的意思是
参数[:types]=[“type1”、“type2”、“type3”…]
并将
拖放到