How to know Array if have key?

a_array= [{“1”=>“11”}, {“2”=>“22”}]
how to test if a_array have a key “11”???

Zhenning G. wrote:

a_array= [{“1”=>“11”}, {“2”=>“22”}]
how to test if a_array have a key “11”???

Arrays don’t have keys, Hashes do.
Here, you have an array with two elements.
Both the elements are hashes. Neither hash has
the key “11”, though the first has it as a value.

If you want to know whether a hash has a value,
it’s slow; you have to search the hash, or extract
all the values and check for the one you care about.

If you have a hash and you want to find whether or not a given value
as a key you can use the “has_key?” method

h = { “a” => 100, “b” => 200 }
h.has_key?(“a”) #=> true
h.has_key?(“z”) #=> false

thank you for help. clear.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs