1, "other_values" => "whatever", "inner_value-6ren">
gpt4 book ai didi

ruby - 使用 delete_if 删除散列的单个值而不是整个散列 RUBY

转载 作者:太空宇宙 更新时间:2023-11-03 18:08:20 25 4
gpt4 key购买 nike

我有一个数组

array_hash = [
{
"array_value" => 1,
"other_values" => "whatever",
"inner_value" => [
{"iwantthis" => "forFirst"},
{"iwantthis2" => "forFirst2"},
{"iwantthis3" => "forFirst3"}
]
},
{
"array_value" => 2,
"other_values" => "whatever2",
"inner_value" => [
{"iwantthis" => "forSecond"},
{"iwantthis2" => "forSecond2"},
{"iwantthis3" => "forSecond3"}
]
},
]

我想删除内部值或将其弹出(我更喜欢弹出)。所以我的输出应该是这样的:

array_hash = [
{
"array_value" => 1,
"other_values" => "whatever"
},
{
"array_value" => 2,
"other_values" => "whatever2"
},
]

我试过delete_if

array_hash.delete_if{|a| a['inner_value'] }

但是它会删除数组中的所有数据。有什么解决办法吗?

最佳答案

试试这个:

array_hash.map{ |a| {'array_value' => a['array_value'], 'other_values' => a['other_values'] }}

关于ruby - 使用 delete_if 删除散列的单个值而不是整个散列 RUBY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39510673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com