[英]how to check if a certain key-value pair exists in array of hashes as json in ruby on rails
I have an array of hashes as json
, so how to check my array of hashes contains a hash with a given key-value pair. 我有一个作为
json
的哈希数组,因此如何检查我的哈希数组包含具有给定键值对的哈希。
This is my json
这是我的
json
[{"question"=>"0a2a3452", "answer"=>"bull"}, {"question"=>"58deacf9", "answer"=>"bullafolo"}, {"question"=>"32c53e5f", "answer"=>"curosit"}, {"question"=>"b5546bcf", "answer"=>""}, {"question"=>"0f0b314", "answer"=>""}]
I tried looping through the json
array, but this is tedious, as I need to check that if that json
has that hash with a given key-value pair 我尝试循环遍历
json
数组,但这很麻烦,因为我需要检查json
是否具有给定键值对的哈希值
It's a questionnaire form, in which I have to perform an update on answers 这是一个调查表,我必须在其中对答案进行更新
if !@client_find.nil?
@client_find.questionnaire
params[:commit].each do |key, value|
@json=[]
@json = @client_find.questionnaire
if !value.empty? && @json.include?(key)
puts "blunderc "+ value.inspect
@new_append = Hash.new
@new_append[:question] = key
@new_append[:answer]= value
@json << @new_append
end
if !key.empty? && !value.empty?
#logic
@json.each do |u|
if (u.key? key)
puts "bothu "+ u[key].inspect
u[key] = value
end
end
end
end
Array#any? Array#any? iterates through the array.
遍历数组。 In each iteration I check wether the current hash has the searched question key or not.
在每次迭代中,我都会检查当前哈希是否具有搜索到的问题关键字。 If a hash is found Array#any?
如果找到哈希,则为Array#any? returns true otherwise false.
返回true,否则返回false。
array = [{"question"=>"0a2a3452", "answer"=>"bull"}, {"question"=>"58deacf9", "answer"=>"bullafolo"}, {"question"=>"32c53e5f", "answer"=>"curosit"}, {"question"=>"b5546bcf", "answer"=>""}, {"question"=>"0f0b314", "answer"=>""}]
search_for_key = '0a2a3452'
array.any?{|hash| hash['question'] == search_for_key}
I'll assume that you want to check the existence of a hash which has the key/value pair "quesetion" => "some-value"
. 我假设您要检查是否存在具有键/值对
"quesetion" => "some-value"
的哈希。
Here's how you can do it: 您可以按照以下方法进行操作:
array.any? { |item| item['question'] == 'some-question-id' }
考虑到您正在检查特定键是否存在
@json.any? {|obj| obj.key?(your_particular_key)
You can filter the array using Enumerable#select
to get only the hashes that contains the desired key. 您可以使用
Enumerable#select
筛选数组,以仅获取包含所需键的哈希值。
filtered = my_hash.select { |item| item['desired_key'] }
That's possible because nil
is falsey. 这是可能的,因为
nil
是错误的。 If you input is a raw JSON you'll need to parse it to a Ruby hash using JSON#parse
or any other equivalent method. 如果您输入的是原始JSON,则需要使用
JSON#parse
或任何其他等效方法将其解析为Ruby哈希。
filtered
will give you all the hashes that contain the desired_key
. filtered
将为您提供所有包含desired_key
的哈希值。
Is that what you want ? 那是你要的吗 ? Btw guitarman's answer is way better !
顺便说一句,吉他手的答案更好!
questions = [{"question"=>"0a2a3452", "answer"=>"bull"}, {"question"=>"58deacf9", "answer"=>"bullafolo"}, {"question"=>"32c53e5f", "answer"=>"curosit"}, {"question"=>"b5546bcf", "answer"=>""}, {"question"=>"0f0b314", "answer"=>""}]
result = questions.find { |question| question['question'] == "Bonour" }
if result.nil?
puts "Not found"
else
puts "#{result['question']} #{result['answer']}"
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.