I
I
Ilnar252017-07-31 20:44:40
Ruby on Rails
Ilnar25, 2017-07-31 20:44:40

Change value in hash?

array = [ { 1 => 5, 23 => 1}, { 2 => 6, 22 => 2} ]
hash = { 1 => 5, 23 => 1}
array <<
hash having a key keys[0] == hash.keys[0] then you need to increase its value by +1.
End result array = [ { 1 => 6 , 23 => 1}, { 2 => 6, 22 => 2} ]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
N. Bekseitov, 2017-08-01
@Ilnar25

Wrote in haste

array = [ { 1 => 5, 23 => 1}, { 2 => 6, 22 => 2} ]
hash = { 1 => 5, 23 => 1}
array = array.map do |x|
  first_key = x.keys[0]
  x[first_key] = x[first_key] + 1 if first_key == hash.keys[0]
  x
end

I
Ilnar25, 2017-08-01
@Ilnar25

Thank you! Both options work...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question