简体   繁体   English

如何访问哈希内的数组?

[英]How can I access an array inside a hash?

I have a hash within an array: 我在数组中有一个哈希:

values = {}
values.merge!(name => {
  "requested_amount" => @specific_last_pending_quota.requested_amount,
  "granted" => @specific_last_pending_quota.granted,
  "pending_final" => pending_final
})
@o_requests[request.receiving_organization][request.program_date][:data] = values

I send it to the view and then when I get it so: 我将其发送到视图,然后在收到时发送:

= quota[:data].inspect
# {"theme"=>{"requested_amount"=>2, "granted"=>false, "pending_final"=>0}}

I want to fetch the object like this: 我想这样获取对象:

= quota[:data]["theme"].inspect

But I got this error 但是我得到这个错误

can't convert String into Integer

Try convert it to proper hash I guess it should work fine. 尝试将其转换为适当的哈希,我猜它应该可以正常工作。

values = {}
 values.merge!(name => {:requested_amount => @specific_last_pending_quota.requested_amount, :granted => @specific_last_pending_quota.granted, :pending_final => pending_final})

@o_requests[request.receiving_organization][request.program_date][:data] = values

I guess quota[:data] may return array of hash 我猜为quota[:data]可能返回哈希数组

Try: 尝试:

= quota[:data][0]["theme"]

Here I tried case to get same error and check: 在这里,我尝试案例以得到相同的错误并检查:

 > h[:data]
 #=> [{"theme"=>{"requested_amount"=>2, "granted"=>false, "pending_final"=>0}}] 
 > h[:data]["theme"]
TypeError: no implicit conversion of String into Integer
    from (irb):10:in `[]'
    from (irb):10
    from /home/ggami/.rvm/rubies/ruby-2.2.1/bin/irb:11:in `<main>'
 > h[:data][0]["theme"]
 #=> {"requested_amount"=>2, "granted"=>false, "pending_final"=>0} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM