0
我在inner_value字段值的長列表,從中我想僅一些值Ruby on Rails的陣列格式 - 除去//選擇哈希數組內從內陣列值
我有陣列的格式如下:
hash_array = [
{
"array_value" => 1,
"inner_value" => [
{"iwantthis" => "forFirst"},
{"iwantthis2" => "forFirst2"},
{"Idontwantthis" => "some value"},
{"iwantthis3" => "forFirst3"},
{"Idontwantthis2" => "some value"},
{"Idontwantthis3" => "some value"},
{"Idontwantthis4" => "some value"},
{"Idontwantthis5" => "some value"},
{"Idontwantthis6" => "some value"},
]
},
{
"array_value" => 2,
"inner_value" => [
{"iwantthis" => "forSecond"},
{"Idontwantthis" => "some value"},
{"iwantthis3" => "forSecond3"},
{"iwantthis2" => "forSecond2"},
{"Idontwantthis2" => "some value"},
{"Idontwantthis3" => "some value"},
{"Idontwantthis4" => "some value"},
{"Idontwantthis5" => "some value"},
{"Idontwantthis6" => "some value"},
]
},
]
所需的輸出:
[
{
"array_value" => 1,
"inner_value" => [
{"iwantthis" => "forFirst"},
{"iwantthis2" => "forFirst2"},
{"iwantthis3" => "forFirst3"}
]
},
{
"array_value" => 2,
"inner_value" => [
{"iwantthis" => "forSecond"},
{"iwantthis2" => "forSecond2"},
{"iwantthis3" => "forSecond3"}
]
},
]
我在這一點,但其過於昂貴的使用運行循環嘗試。
所以,我想是這樣的:
hash_array.select { |x| x["inner_value"].select {|y| !y["iwantthis"].nil? } }
,但是這不工作或者..
注:訂單/排序不要緊
你沒有。首先,我是說如果有什麼索引未知,但關鍵是已知的? – user1735921
@ user1735921讓我更新使用已知的密鑰 –
好吧,如果它的多個值,如假設有「iwanthis2」和「iwantthis3」 – user1735921