You obviously have to iterate at least once over the elements of your array.
If you do this often, you should use another data format:
array1 = [ { ID: '1', value: 'abc' }, { ID: '2', value: 'def' }, { ID: '3', value: 'abc' } ] lookup_table = array1.each_with_object(Hash.new { |h, k| h[k] = [] }) do |hash, table| table[hash[:value]] << hash[:ID] end p lookup_table # {"abc"=>["1", "3"], "def"=>["2"]} p lookup_table['abc'] # ["1", "3"]
As a bonus, it gives you all the IDs for which this value is found, and it gives it fast.
If you just want to know if there's a value somewhere in your hashes, you could use a Set:
require 'set' all_values = Set.new(array1.map{|h| h[:value]}) p all_values.include? 'abc' # true p all_values.include? 'xyz' # false
Again, the lookup will be much faster than with an array of hashes.
O(1)point of lookup for hash tables. Just use a JS Object literal for this.array1come from, how is it generated? Is it possible to change the structure (for example to an hash) at the origin ofarray1?"1"to be a value inside a hash of your array?