gpt4 book ai didi

ruby-on-rails - 删除 ruby​​ 数组中的特殊字符

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

我有一个如下所示的数组:

[
[
"[\"\", \"Mrs. Brain Bauch\", \"Vernice Ledner\"]",
"[\"\", \"Robb Ratke\", \"Amaya Jakubowski\"]",
"[\"\", \"Lindsey Cremin III\", \"Harvey Fisher\"]",
"[\"\", \"Daniela Schneider\", \"Benny Schumm\"]"
]
]

如何将其转换为如下所示的数组结构:

[
[
["Mrs. Brain Bauch", "Vernice Ledner"],
["Robb Ratke", "Amaya Jakubowski"],
["Lindsey Cremin III", "Harvey Fisher"],
["Daniela Schneider", "Benny Schumm"]
]
]

最佳答案

require 'json'
input = [[
"[\"\", \"Mrs. Brain Bauch\", \"Vernice Ledner\"]",
"[\"\", \"Robb Ratke\", \"Amaya Jakubowski\"]",
"[\"\", \"Lindsey Cremin III\", \"Harvey Fisher\"]",
"[\"\", \"Daniela Schneider\", \"Benny Schumm\"]"
]]
[input.first.map { |l| JSON.parse l }.map { |a| a.reject &:empty? }]
#⇒ [[
#   ["Mrs. Brain Bauch", "Vernice Ledner"],
#  ["Robb Ratke", "Amaya Jakubowski"],
#  ["Lindsey Cremin III", "Harvey Fisher"],
#  ["Daniela Schneider", "Benny Schumm"]
#  ]]

关于ruby-on-rails - 删除 ruby​​ 数组中的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36997342/

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