2

请参阅以下哈希:

params[:category] #=> {"category"=>{"name"=>"name1", "parent_id"=>1, "category"=>{"name"=>"name2"}}}

和所需的输出:

params[:category] #=> {"category"=>{"name"=>"name1", "parent_id"=>1 }}

我尝试了几件事,但都没有成功

params[:category].delete(params[:category][...]

如何获取此嵌套哈希的键/值对以将其删除?

谢谢你的帮助

4

3 回答 3

4

如果哈希是:

params[:category] = {"category"=>{"name"=>"name1", "parent_id"=>1, "category"=>{"name"=>"name2"}}}

然后使用:

params[:category]['category'].delete('category')
params[:category]
=> {"category"=>{"name"=>"name1", "parent_id"=>1}}
于 2013-01-18T08:59:17.740 回答
0
h = {"category"=>{"name"=>"name1", "parent_id"=>1, "category"=>{"name"=>"name2"}}}
h['category'].delete('category')
{"category"=>{"name"=>"name1", "parent_id"=>1}}

第三行是结果。您想删除category的键h['category']吗?

于 2013-01-18T08:58:48.480 回答
0

如果哈希是:

params[:category] = {"category"=>{"name"=>"name1", "parent_id"=>1, "category"=>{"name"=>"name2"}}}

然后使用:

params[:category]["category"].slice!("category")
于 2013-01-18T09:04:50.110 回答