ruby-on-rails 使用Ruby on Rails从JSON格式合并对象并添加键

bkhjykvo  于 2022-11-19  发布在  Ruby
关注(0)|答案(2)|浏览(199)

我有一个被查询的数组列表,需要根据对象与相同的location_id合并。

这是生成数组的代码

filled  =  Product.on_hand_location(pid).to_a
       empty   =  Product.on_hand_location_empty_cylinder(pid).to_a
       data    = filled + empty 
  
 
       result = data.map{ |k| 
        { 
        details: { 
              location_id: k['location_id'],
              "location_name"=>k['location_name'],
              "onhandcylynder"=>k['onhand'] == nil ?  0 :  k['onhand'],
              "emptycylynder"=> k['emptyonhand'] == nil ?  0 :  k['emptyonhand']
           } ,
        }
     }
    
    respond_with [ onhand: result ]

下面的JSON格式是上面代码的输出,其中包含需要合并的location_id

[{
    "onhand": [{
            "details": {
                "location_id": 1,
                "location_name": "Capitol Drive",
                "onhandcylynder": "4.0",
                "emptycylynder": 0
            }
        },
        {
            "details": {
                "location_id": 2,
                "location_name": "SM City Butuan",
                "onhandcylynder": "5.0",
                "emptycylynder": 0
            }
        },
        {
            "details": {
                "location_id": 1,
                "location_name": null,
                "onhandcylynder": 0,
                "emptycylynder": "2.0"
            }
        }
    ]
  }]

我想要的输出

[{
     "onhand": [{
        "details": {
            "location_id": 1,
            "location_name": "Capitol Drive",
            "onhandcylynder": "4.0",
            "emptycylynder": 0
        }
    },
    {
        "details": {
            "location_id": 2,
            "location_name": "SM City Butuan",
            "onhandcylynder": "5.0",
            "emptycylynder": "2.0"
        }
    }
   ]
 }]
w6mmgewl

w6mmgewl1#

我觉得你应该试试

data = Product.on_hand_location(pid).to_a
empty = Product.on_hand_location_empty_cylinder(pid).to_a
empty.each do |location|
  data.push(location) if data.none? { |item| item['location_id'] == product['location_id'] }
end 
 
result = ...

hash = {}
Product.on_hand_location_empty_cylinder(pid).map { |l| hash[l['location_id']] = l }
Product.on_hand_location(pid).map { |l| hash[l['location_id']] = l }
data = hash.values

如果您需要这两个查询中的一些数据,则应该尝试

hash = {}
Product.on_hand_location_empty_cylinder(pid).map { |l| hash[l['location_id']] = l }
Product.on_hand_location(pid).map { |l| hash[l['location_id']].merge!(l) }
data = hash.values

以便就地合并

s5a0g9ez

s5a0g9ez2#

我重构了我的代码并能够得到我想要的结果

filled  =  Product.on_hand_location(pid).to_a
  emptyTank =  Product.on_hand_location_empty_cylinder(pid).to_a
  data = filled + emptyTank 
  
  cylinder = data.map(&:dup)
  .group_by { |e| e.delete('location_id') }

   finalResult = cylinder.map{|_, location_id | location_id.reduce({}) { |result, location_id|
     result.merge(location_id)
  } }

  respond_with [ onhand: finalResult]

结果已与相同的位置ID合并,并且emptyonhand键已在对应于其位置ID的位置中合并

[
{
    "onhand": [
        {
            "onhand": "1.0",
            "location_name": "Capitol Drive",
            "emptyonhand": "5.0"
        },
        {
            "onhand": "5.0",
            "location_name": "SM City Butuan"
        }
    ]
}

]

相关问题