0

Could someone point me in the right direction about looping through an array and extracting the required values. It's causing me issues understanding it:

array = [{"response_message"=>nil, "response_client_message"=>nil, "response_customer_message"=>nil, "closed"=>true, "updated_at"=>2012-05-30 13:20:49 UTC, "created_at"=>2012-05-30 13:20:29 UTC, "token"=>"2fda85eab962fa6e27850605f2f948ca", "price"=>"$24.00", "amount"=>#<BigDecimal:7fa3f4485428,'0.24E2',9(18)>, "currency_code"=>"USD", "metadata"=>"{\"xxx\": 5, \"xxx\": 250, \"xxx\": true, \"support\": { \"email\": true, \"phone\": false } }", "line_items"=>[{"amount"=>#<BigDecimal:7fa3f4482fe8,'0.24E2',9(18)>, "notes"=>nil, "currency_code"=>"USD", "description"=>"1 day", "price"=>"$24.00", "feature_level"=>"{\"hotspots\": 5, \"vouchers\": 250, \"customizable_logins\": true, \"support\": { \"email\": true, \"phone\": false } }", "metadata"=>"{\"hotspots\": 5, \"vouchers\": 250, \"customizable_logins\": true, \"support\": { \"email\": true, \"phone\": false } }"}]}]

One of these is generated each day, I need to loop through and present a few values. I've tried this:

array.select{|elem| elem[:updated_at]}

But that gives me a [].

How do I loop through this and extract the values? I also need to understand how to get the line_items array out too.

Thanks

0

3 Answers 3

2
array.select{|elem| elem["updated_at"]}

pass string not a symbol

Sign up to request clarification or add additional context in comments.

Comments

1

It's not entirely clear what you're after, but if you want to extract values from that array, try map:

array.map { |elem| elem["updated_at"] }

This will return you a list of all the "updated_at" values.

Comments

0

if you want to loop through an array and collect the updated_at values, then you should use the collect/map method. In your example you are referencing elem[:updated_at] which is not the same as elem["updated_at"]

array.collect{|elem| elem["updated_at"]}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.