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.
I would start with something like this:
array.map do |element|
[element.name, [element.value.quantity, element.value.quality]]
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.