简体   繁体   中英

Cleanest way to check if a key exists on an embedded hash inside an embedded array

I'm trying to figure out a good way to find whether or not an error_body is present on the resq in this hash.

hash_v = {"brand"=>[{ "model_id"=>["1"], "resq"=>[{"error_body"=>[{"message"=>["Error"]}]}]}]}

This is the cleanest way that I've seen so far, but I'm wondering if there's a better way, perhaps by using dig , to make this a bit cleaner.

hash_v['brand'].map { |m| m['resq'].first['error_body'] }

Any suggestions?

我不确定使用dig是否是检查键在带有数组的嵌入式哈希中是否存在的最干净的方法,但是此解决方案可能有效:

 hash_v.dig("brand", 0, "resq", 0, "error_body")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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