如何从红宝石上的哈希数组中提取ID?

问题描述 投票:0回答:3

给出:

data = [
  {"id"=>nil, "votable_id"=>1150, "user_ids"=>"1120,1119,1118,1117,1116,1115,1114,1113,1112,1111,1110,1109,1108,1107"},
  {"id"=>nil, "votable_id"=>1151, "user_ids"=>"1120,1119,1118,1117,1116,1115,1114,1113,1112,1111,1110,1109,1108,1107"}
]

我希望返回字符串g["user_ids"]中所有整数的唯一表示的数组,该数组表示为g元素的所有散列data;即,>

["1120", "1119", "1118", "1117", "1116", "1115", "1114",
 "1113", "1112", "1111", "1110", "1109", "1108", "1107"]

给出:data = [{“ id” => nil,“ votable_id” => 1150,“ user_ids” =>“ 1120,1119,1118,1117,1116,1115,1114,1113,1112,1111,1110, 1109,1108,1107“},{” id“ => nil,” votable_id“ => 1151,” user_ids“ => ...

ruby-on-rails arrays ruby hash
3个回答
2
投票

如果使用Rails,请尝试以下操作:


2
投票

要获得唯一的ID:


0
投票
String#split
© www.soinside.com 2019 - 2024. All rights reserved.