我有一个对象数组,并试图减少到特定形式的数组。例如:
[
#<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>>
]
应该减少到
[ [Item1, [val1, val2]], [Item2, [val1, val2]], [Item3, [val1, val2]], [Item4,
[val1, val2]], [Item5, [val1, val2]]]
我努力了
arr1 = []
arr2 = []
array.each do |array|
arr1 << array.name
arr2 << array.value.quantity
arr2 << array.value.quality
arr1 << arr2
end
上面代码的输出是
[ 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]....]
这段代码的问题它不会停止每个对象的迭代,我的意思是对于 Item1,它应该只有 val1 和 val2 仅适用于 Item1。