EDITED: 我们如何采取以下方式
fields = [{"Name"=>"CONTACT.MAILADDRESS.LATITUDE", "dataName"=>"Pt_FieldDouble", "externalDataName"=>"number", "isRequired"=>"false", "isReadOnly"=>"false", "maxLength"=>"10", "DisplayLabel"=>"Latitude", "Description"=>"Latitude", "id"=>"999505900000211"},
{"Name"=>"CONTACT.MAILADDRESS.LONGITUDE", "dataName"=>"Pt_FieldDouble", "externalDataName"=>"number", "isRequired"=>"false", "isReadOnly"=>"false", "maxLength"=>"11", "DisplayLabel"=>"Longitude", "Description"=>"Longitude", "id"=>"999505900000212"},
{"Name"=>"CONTACT.STATE", "dataName"=>"Pt_FieldString", "externalDataName"=>"string", "isRequired"=>"false", "isReadOnly"=>"false", "maxLength"=>"0", "DisplayLabel"=>"Status", "Description"=>"Active/Inactive", "id"=>"999505900000166"},
{"Name"=>"XXSTATUS", "dataName"=>"Pt_FieldString", "externalDataName"=>"string", "isRequired"=>"false", "isReadOnly"=>"false", "maxLength"=>"0", "DisplayLabel"=>"XStatus", "Description"=>"Active/Inactive", "id"=>"99950591200166"}]
output =
[{"CONTACT" =>
{ "MAILADDRESS" =>
{ "Name" => "LATITUDE", "dataName"=>"Pt_FieldDouble", "externalDataName"=>"number", "isRequired"=>"false", "isReadOnly"=>"false", "maxLength"=>"10", "DisplayLabel"=>"Latitude", "Description"=>"Latitude", "id"=>"999505900000211"},
{ "Name" => "LATITUDE", "dataName"=>"Pt_FieldDouble", "externalDataName"=>"number", "isRequired"=>"false", "isReadOnly"=>"false", "maxLength"=>"11", "DisplayLabel"=>"Longitude", "Description"=>"Longitude", "id"=>"999505900000212"}},
{ "Name" =>"STATE", "dataName"=>"Pt_FieldString", "externalDataName"=>"string", "isRequired"=>"false", "isReadOnly"=>"false", "maxLength"=>"0", "DisplayLabel"=>"Status", "Description"=>"Active/Inactive", "id"=>"999505900000166"}},
{ "Name" =>"XXSTATUS", "dataName"=>"Pt_FieldString", "externalDataName"=>"string", "isRequired"=>"false", "isReadOnly"=>"false", "maxLength"=>"0", "DisplayLabel"=>"XStatus", "Description"=>"Active/Inactive", "id"=>"99950591200166"}]
我试过
fields.map {| i | {i ["姓名"] => i.except("名称")}}
[{"CONTACT.MAILADDRESS.LATITUDE"=>{"dataName"=>"Pt_FieldDouble", "externalDataName"=>"number", "isRequired"=>"false", "isReadOnly"=>"false", "maxLength"=>"10", "DisplayLabel"=>"Latitude", "Description"=>"Latitude", "id"=>"999505900000211"}},
{"CONTACT.MAILADDRESS.LONGITUDE"=>{"dataName"=>"Pt_FieldDouble", "externalDataName"=>"number", "isRequired"=>"false", "isReadOnly"=>"false", "maxLength"=>"11", "DisplayLabel"=>"Longitude", "Description"=>"Longitude", "id"=>"999505900000212"}},
{"CONTACT.STATUS"=>{"dataName"=>"Pt_FieldString", "externalDataName"=>"string", "isRequired"=>"false", "isReadOnly"=>"false", "maxLength"=>"0", "DisplayLabel"=>"Status", "Description"=>"Active/Inactive", "id"=>"999505900000166"}}]
了解如何拆分键并将值放在一起
答案 0 :(得分:2)
您想要的内容与Trie类似,因此您可以解析名称部分并使用each_with_object
和inject
创建嵌套哈希。这适用于任何嵌套深度:
[{
"column1Value": "Country",
"column2Value": "Value"
}, {
"column1Value": "Flag",
"column2Value": "Value"
}, {
"column1Value": "Capital",
"column2Value": "Value"
}, {
"column1Value": "Region",
"column2Value": "Value"
}, {
"column1Value": "GPS",
"column2Value": "Value"
}]
答案 1 :(得分:1)
有点罗嗦,但你可以做......
output = {}
pointer = nil
fields.each do |field|
pointer = output
sections = field['Name'].split('.').each do |section|
pointer[section] ||= {}
pointer = pointer[section]
end
pointer.merge!(field)
pointer.delete('Name')
end
output