I can't seem to wrap my head around the following problem today. I am parsing a JSON response from an API that helps me plan how many boxes my items will fill up to fulfill a shipment.
I've saved and parsed the JSON response from the API to a variable called API_response in Rails below. Now I specifically need to count each time "id"=>"Bin1" shows up in this response.
I'm thinking that maybe the best way to do this is to do a select for "id"=>"Bin1" and map it to an array each time and then count the number of indexes in the array so that I have a final count for how many boxes I'll use? How would I go about doing this?
API_response = {"response"=>{"id"=>"1538005707_bc789275d7cc93eca86830e41a44f7a9", "bins_packed"=>[{"bin_data"=>{"w"=>12, "h"=>6, "d"=>12, "id"=>"Bin1", "used_space"=>57.8704, "weight"=>80, "used_weight"=>100, "stack_height"=>5, "order_id"=>"unknown"}, "items"=>[{"id"=>12, "w"=>10, "h"=>5, "d"=>10, "wg"=>80}]}, {"bin_data"=>{"w"=>12, "h"=>6, "d"=>12, "id"=>"Bin1", "used_space"=>57.8704, "weight"=>80, "used_weight"=>100, "stack_height"=>5, "order_id"=>"unknown"}, "items"=>[{"id"=>12, "w"=>10, "h"=>5, "d"=>10, "wg"=>80}]}, {"bin_data"=>{"w"=>12, "h"=>6, "d"=>12, "id"=>"Bin1", "used_space"=>57.8704, "weight"=>80, "used_weight"=>100, "stack_height"=>5, "order_id"=>"unknown"}, "items"=>[{"id"=>12, "w"=>10, "h"=>5, "d"=>10, "wg"=>80}]}], "errors"=>[], "status"=>1, "not_packed_items"=>[]}}
UPDATE
I think what I'd actually really love to do is, for each occurrence of Bin1, push the weight of the bin into a new array. So I'd like my final result from my response example above to be an array such as Bin1_Array = [80, 80, 80]
Bin1occurs, or the sum of size ofitemsarray for when the id equalsBin1?