I have an array of hash map. It looks like this:
params = [] CSV.foreach(......) do one_line_item = {} one_line_item[:sku] = "Hello" one_line_item[:value] = "20000" params << one_line_item end I want to check if :sku is in this array of hash or not. I am doing it like this:
# Reading new line of csv in a hash and saving it in a temporary variable (Say Y) params.each do |p| if p[:sku] == Y[:sku] next end end I am iterating through the complete list for every value of sku, and thus time complexity is going for a toss [O(n^2)], need less to say it is useless.
Is there a way I can use include??
If I can get an array of values corresponding to the key :sku from the whole array in one shot, it would solve my problem. (I know I can maintain another array for these values but I want to avoid that)
One example of params
params = [{:sku=>"hello", :value=>"5000"}, {:sku=>"world", :value=>"6000"}, {:sku=>"Hi", :value=>"7000"}]
params?