简体   繁体   English

转换此哈希的最佳方法是什么?

[英]What would be the best way to convert this hash?

I have a Ruby hash that I need to convert to another format. 我有一个Ruby哈希,我需要将其转换为另一种格式。 Considering that the 'array' size is unknown/unlimited, how would you 'flatten' this hash into the desired format? 考虑到“数组”的大小是未知/无限制的,您将如何将该哈希“平化”为所需格式?

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'
  }
}

If you can make a few assumptions about the structure of the input ie 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

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"}} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 json 格式的键值对转换为以符号为键的 ruby​​ 哈希的最佳方法是什么? - what is the best way to convert a json formatted key value pair to ruby hash with symbol as key? 修改params嵌套哈希的最佳方法是什么? - What is the best way to modify params nested hash? 在Rails中,将哈希压缩为嵌套哈希的最佳方法是什么 - In Rails, what is the best way to compact a hash into a nested hash 使模型访问Rails中的params哈希的最佳方法是什么? - What is the best way to give a model access to params hash in Rails? 在Rails中组织以下协会的最佳方法是什么? - What would be the best way to organise the following associations in Rails? 将AngularJS与Ruby on Rails一起使用的最佳方法是什么? - What would be the best way to use AngularJS with Ruby on Rails? 什么是测试流程(跨控制器)的最佳方法? - What would be the best way to test a flow (cross controllers)? 在Rails 4中,将当前时区中的Date转换为DateTime的最佳方法是什么? - What's the best way to convert a Date to a DateTime in the current timezone in Rails 4? 在Ruby或Rails中,将美国东部时间转换为UTC的最佳方法是什么? - In Ruby or Rails, what is the best way to convert Eastern Time to UTC? Rails确定两个(或更多)给定URL(作为字符串或哈希选项)是否相等的最佳方法是什么? - What is the best way in Rails to determine if two (or more) given URLs (as strings or hash options) are equal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM