Skip to content

Instantly share code, notes, and snippets.

@regedarek
Created August 1, 2012 10:49
Show Gist options
  • Save regedarek/084a5967011df6e91b36 to your computer and use it in GitHub Desktop.
Save regedarek/084a5967011df6e91b36 to your computer and use it in GitHub Desktop.
Find in array of hashes
items = [
{
:condition => "NewItem",
:seller_sku => "bsgnpoprod_142829029X_41",
:asin => "142829029X",
:quantity => "1"
},
{
:condition => "UsedGood",
:seller_sku => "bsgnpoprod_142829029X_42",
:asin => "142829029X",
:quantity => "1"
},
{
:condition => "UsedGood",
:seller_sku => "bsgnpoprod_142829029X_43",
:asin => "142829029X",
:quantity => "1"
},
{
:condition => "UsedGood",
:seller_sku => "bsgnpoprod_1403975078_44",
:asin => "1403975078",
:quantity => "1"
}
]
@regedarek
Copy link
Author

items = [ [ { :condition => "NewItem", :seller_sku => "bsgnpoprod_142829029X_41", :asin => "142829029X", :quantity => "1" }, { :condition => "UsedGood", :seller_sku => "bsgnpoprod_142829029X_42", :asin => "142829029X", :quantity => "1" }, { :condition => "UsedGood", :seller_sku => "bsgnpoprod_142829029X_43", :asin => "142829029X", :quantity => "1" }, { :condition => "UsedGood", :seller_sku => "bsgnpoprod_1403975078_44", :asin => "1403975078", :quantity => "1" } ] ]

'1403975078'  => 1
'142829029X'  => 3

@regedarek
Copy link
Author

>> items.flatten.count {|h| h[:asin] == '1403975078'}
1
>> items.flatten.count {|h| h[:asin] == '142829029X'}
3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment