john simons
john simons

Reputation: 81

how to update an array in array of objects

I have following output available in a variable test

#<someobject customer=[#<someobject product=[#<someobject id='ABC123'>, #<someobject id=''>], id='ADE343'>]>

I am trying to convert its result as follows:

#<someobject customer=[#<someobject product=['ABC123','DEF143'], id='ADE343'>]>

I can achieve that using following but looks like overkill

test1 = test.customer.map { |p| p.product }.flatten.map { |e| e.id }
test.customer.map { |p| p.product = test1 }
test

Is there any better way to do this?

Upvotes: 2

Views: 68

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52357

test.customer.tap { |obj| obj.product.map!(&:id) }

Upvotes: 2

Related Questions