-1

このような配列:

a = ["<root>", 
"<products>", 
"<product>", 
"<id>", "1", "</id>",
"<name>", "name1", "</name>", 
"</product>",
"<product>", 
"<id>", "2", "</id>",
"<name>", "name2", "</name>", 
"</product>",
"</products>", 
"</root>"]

そしてこのようなハッシュ:

b = {
"products" => {
  "product" => [
     {"id" => "1", "name" => "name1"},
     {"id" => "2", "name" => "name2"}
   ]
  }
}

このハッシュを取得する方法cab</ p>

c = {
"products" => {
  "product" => [
     {"id" => "1", "name" => "name1", "id_index" => 4, "name_index" => 7},
     {"id" => "2", "name" => "name2", "id_index" => 12, "name_index" => 15}
   ]
  }
}

4、7、12、15は、配列内の「1」、「name1」、「2」、「name2」のインデックスaです。

4

1 に答える 1

0

私はそれが次のように見えると思います:

c = {'products' => {'product' => b['products']['product']
  .map{|o| o.merge({'id_index' => a.index(o['id']), 'name_index' => a.index("name#{o['id']}")})}}}
于 2012-06-25T08:59:09.703 に答える