I have a Ruby hash that I need to convert to another format. Considering that the 'array' size is unknown/unlimited, how would you 'flatten' this hash into the desired format?
我有一个Ruby哈希,我需要转换为另一种格式。考虑到“数组”大小未知/无限制,您如何将此哈希“压扁”为所需格式?
Original Hash
原始的哈希
{
:parameters => {
:'userResponse.objectInstanceType' => 'QuesAnsResponse',
:'userResponse.quesAnsDetailArray' => {
:'0' => {
'.answer'=> 'Texas'
},
:'1' => {
'.answer' => 'w3schools'
}
}
}
}
Desired Format:
期望的格式:
{
:parameters => {
:'userResponse.objectInstanceType' => 'QuestionAnsResponse',
:'userResponse.quesAnsDetailArray[0].answer' => 'Texas',
:'userResponse.quesAnsDetailArray[1].answer' => 'w3schools'
}
}
2 个解决方案
#1
2
If you can make a few assumptions about the structure of the input i.e. that it has the numbered responses inside a "userResponse.quesAnsDetailArray"
section then you could do something along these lines:
如果你可以对输入的结构做一些假设,即它在“userResponse.quesAnsDetailArray”部分中有编号的响应,那么你可以沿着这些方向做一些事情:
new_hash = { 'parameters' =>
{ 'userResponse.objectInstanceType' => 'QuestionAnsResponse' } }
hash['parameters']['userResponse.quesAnsDetailArray'].each_pair do |index, details|
details.each_pair do |field, value|
new_hash['parameters']["userResponse.quesAnsDetailArray[#{index}]#{field}"] = value
end
end
#2
1
I have assumed the hash is as follows:
我假设哈希如下:
h = {
'parameters'=> {
'userResponse.objectInstanceType'=> 'QuesAnsResponse',
'userResponse.quesAnsDetailArray'=> {
'0'=> {
'.answer'=> '',
'.answerFieldType'=> 'text',
'.isRequired'=> 'true',
'.metaData'=> 'QUESTION_1',
'.questionFieldType'=> 'label',
'.question'=> 'What is the name of your state?'
},
'1'=> {
'.answer'=> '',
'.answerFieldType'=> 'text',
'.isRequired'=> 'true',
'.metaData'=> 'QUESTION_2',
'.questionFieldType'=> 'label',
'.question'=> 'What is the name of your first school'
}
}
}
}
and would convert it to the desired format as follows:
并将其转换为所需的格式,如下所示:
{ 'parameters'=> Hash[[
['userResponse.objectInstanceType',
h['parameters']['userResponse.objectInstanceType']],
*h['parameters']['userResponse.quesAnsDetailArray'].
flat_map { |ndx,f|
["userResponse.quesAnsDetailArray[#{ndx}]"].product(f.to_a) }.
map { |prefix,(suffix,value)| [prefix+suffix, value] } ]]
}
#=> {"parameters"=>
{"userResponse.objectInstanceType"=>"QuesAnsResponse",
"userResponse.quesAnsDetailArray[0].answer"=>"",
"userResponse.quesAnsDetailArray[0].answerFieldType"=>"text",
"userResponse.quesAnsDetailArray[0].isRequired"=>"true",
"userResponse.quesAnsDetailArray[0].metaData"=>"QUESTION_1",
"userResponse.quesAnsDetailArray[0].questionFieldType"=>"label",
"userResponse.quesAnsDetailArray[0].question"=>
"What is the name of your state?",
"userResponse.quesAnsDetailArray[1].answer"=>"",
"userResponse.quesAnsDetailArray[1].answerFieldType"=>"text",
"userResponse.quesAnsDetailArray[1].isRequired"=>"true",
"userResponse.quesAnsDetailArray[1].metaData"=>"QUESTION_2",
"userResponse.quesAnsDetailArray[1].questionFieldType"=>"label",
"userResponse.quesAnsDetailArray[1].question"=>
"What is the name of your first school"}}
#1
2
If you can make a few assumptions about the structure of the input i.e. that it has the numbered responses inside a "userResponse.quesAnsDetailArray"
section then you could do something along these lines:
如果你可以对输入的结构做一些假设,即它在“userResponse.quesAnsDetailArray”部分中有编号的响应,那么你可以沿着这些方向做一些事情:
new_hash = { 'parameters' =>
{ 'userResponse.objectInstanceType' => 'QuestionAnsResponse' } }
hash['parameters']['userResponse.quesAnsDetailArray'].each_pair do |index, details|
details.each_pair do |field, value|
new_hash['parameters']["userResponse.quesAnsDetailArray[#{index}]#{field}"] = value
end
end
#2
1
I have assumed the hash is as follows:
我假设哈希如下:
h = {
'parameters'=> {
'userResponse.objectInstanceType'=> 'QuesAnsResponse',
'userResponse.quesAnsDetailArray'=> {
'0'=> {
'.answer'=> '',
'.answerFieldType'=> 'text',
'.isRequired'=> 'true',
'.metaData'=> 'QUESTION_1',
'.questionFieldType'=> 'label',
'.question'=> 'What is the name of your state?'
},
'1'=> {
'.answer'=> '',
'.answerFieldType'=> 'text',
'.isRequired'=> 'true',
'.metaData'=> 'QUESTION_2',
'.questionFieldType'=> 'label',
'.question'=> 'What is the name of your first school'
}
}
}
}
and would convert it to the desired format as follows:
并将其转换为所需的格式,如下所示:
{ 'parameters'=> Hash[[
['userResponse.objectInstanceType',
h['parameters']['userResponse.objectInstanceType']],
*h['parameters']['userResponse.quesAnsDetailArray'].
flat_map { |ndx,f|
["userResponse.quesAnsDetailArray[#{ndx}]"].product(f.to_a) }.
map { |prefix,(suffix,value)| [prefix+suffix, value] } ]]
}
#=> {"parameters"=>
{"userResponse.objectInstanceType"=>"QuesAnsResponse",
"userResponse.quesAnsDetailArray[0].answer"=>"",
"userResponse.quesAnsDetailArray[0].answerFieldType"=>"text",
"userResponse.quesAnsDetailArray[0].isRequired"=>"true",
"userResponse.quesAnsDetailArray[0].metaData"=>"QUESTION_1",
"userResponse.quesAnsDetailArray[0].questionFieldType"=>"label",
"userResponse.quesAnsDetailArray[0].question"=>
"What is the name of your state?",
"userResponse.quesAnsDetailArray[1].answer"=>"",
"userResponse.quesAnsDetailArray[1].answerFieldType"=>"text",
"userResponse.quesAnsDetailArray[1].isRequired"=>"true",
"userResponse.quesAnsDetailArray[1].metaData"=>"QUESTION_2",
"userResponse.quesAnsDetailArray[1].questionFieldType"=>"label",
"userResponse.quesAnsDetailArray[1].question"=>
"What is the name of your first school"}}