I have the following Ruby hash key-value pairs:
[
{
"trait_type"=>"Status",
"value"=>"Unbuilt",
"display_type"=>nil,
"max_value"=>nil,
"trait_count"=>4866,
"order"=>nil
}
]
What I need to check is see if the following key-value pairs are both present:
{
"value"=>"Unbuilt",
"trait_type"=>"Status"
}
Essentially wanting something to the effect of...
traits = [{"trait_type"=>"Status", "value"=>"Unbuilt", "display_type"=>nil, "max_value"=>nil, "trait_count"=>4866, "order"=>nil}]
filter_traits = {"value"=>"Unbuilt", "trait_type"=>"Status"}
traits.include? filter_traits
If you are using ruby >= 2.3, there is a fancy new Hash >= Hash operation that is conceptually similiar to a hypothetical contains?
Using your traits array:
trait = traits[0]
trait >= {"trait_type" => "Status", "value" => "Unbuilt"}
# => true
trait >= {"trait_type" => "Status", "value" => "Built"}
# => false
So you could try something like:
traits.select{|trait|
trait >= filter_traits
}.length > 0
# => true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With