O
O
Ord EO2020-10-05 01:56:54
ruby
Ord EO, 2020-10-05 01:56:54

How to add hash values ​​to an array?

There is a hash like this

[{"amount"=>"5000.00"}, {"amount"=>"250.00"}, {"amount"=>"($200.00)"}, {"amount"=>"250.00"}, {"amount"=>"300.00"}, {"amount"=>"1200.00"},


How to add the values ​​of each amount to the array, the result should be like this:
arr = [5000.00, 200.00, 250.00, 300.00, 1200.00]
Additional complexity is that it amountcan look like this ($200.00)and here it needs to be processed correctly, but I don’t understand how to do it. I will be grateful for any help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-10-05
@OrdeO

arr = [ {"amount"=>"5000.00"} , {"amount"=>"250.00"}, {"amount"=>"($200.00)"}, {"amount"=>"250.00"}, {"amount"=>"300.00"}, {"amount"=>"1200.00"} ]
arr.map(&:values).flatten.map { |i|  i.delete('()$') } # => ["5000.00", "250.00", "200.00", "250.00", "300.00", "1200.00"]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question