0

I have got an array of objects in object and trying to reduce to specific form of array. For example:

[
  #<Item name: "Item 1", content: #<Item value: #<Item quantity: val1>, #<Item 
quality: val2>>,
  #<Item name: "Item 2", content: #<Item value: #<Item quantity: val1>, #<Item 
quality: val2>>,
  #<Item name: "Item 3", content: #<Item value: #<Item quantity: val1>, #<Item 
quality: val2>>,
  #<Item name: "Item 4", content: #<Item value: #<Item quantity: val1>, #<Item 
quality: val2>>,
  #<Item name: "Item 5", content: #<Item value: #<Item quantity: val1>, #<Item 
quality: val2>>
]

should be reduce to

[ [Item1, [val1, val2]], [Item2, [val1, val2]], [Item3, [val1, val2]], [Item4, 
[val1, val2]], [Item5, [val1, val2]]]

I have tried

arr1 = []
arr2 = []
array.each do |array|
 arr1 << array.name
  arr2 << array.value.quantity
   arr2 << array.value.quality
   arr1 << arr2
 end

The output of the above code is

[ Item1,  [val1,val2,val1,val2,val1,val2,val1,val2,val1,val2,val1,val2], 
Item2, [[val1,val2,val1,val2,val1,val2,val1,val2,val1,val2,val1,val2]....]

The problem with this code it does not stop iterate per object what i mean is for Item1 it should be only val1 and val2 only for Item1.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
rekha
  • 27
  • 3

1 Answers1

5

I would start with something like this:

array.map do |element|
  [element.name, [element.value.quantity, element.value.quality]]
end
spickermann
  • 100,941
  • 9
  • 101
  • 131