合并Ruby中的两个对象数组

时间:2022-07-26 12:20:12

I have an array of objects with unique IDs:

我有一个具有唯一id的对象数组:

[{id: 1, score: 33}, {id: 23, score: 50}, {id:512, score: 27}, ...]

I also have an array of User records with matching IDs. The user records have "name" but not "score":

我还有一个具有匹配id的用户记录数组。用户记录有“姓名”而没有“分数”:

[{id: 1, name: "Jon"}, {id: 23, name: "Tom"}, {id: 512, name: "Joey"}, ...]

How can I create a single array with each id, name, and score?

如何创建具有每个id、名称和分数的单个数组?

[{id: 1, name: "Jon", score: 33}, {id: 23, name: "Tom", score: 50}, {id: 512, name: "Joey", score: 27}, ...]

I tried merge, combine, filter, etc but haven't found the Ruby function to accomplish this.

我尝试了合并、合并、筛选等等,但是还没有找到Ruby函数来实现这一点。

3 个解决方案

#1


3  

Assuming that in users there is always record with corresponding :id from scores:

假设在用户中总是有相应的记录:id来自分数:

scores = [{id: 1, score: 33}, {id: 23, score: 50}, {id:512, score: 27}]
users  = [{id: 1, name: "Jon"}, {id: 23, name: "Tom"}, {id: 512, name: "Joey"}]

scores = scores.map { |score| score.merge(users.find { |user| user[:id] == score[:id] }) }
# => [{:id=>1, :score=>33, :name=>"Jon"}, {:id=>23, :score=>50, :name=>"Tom"}, {:id=>512, :score=>27, :name=>"Joey"}]

Hope that puts you in proper direction!

希望这能给你一个正确的方向!

#2


1  

You can use an intermediate Hash.

可以使用中间哈希。

hsh = Hash[ a1.map {|h| [h[:id], h[:score]]} ]
# => {1=>33, 23=>50, 512=>27}
a2.map {|h| h[:score] = hsh[h[:id]]; h}
# => [{:id=>1, :name=>"Jon", :score=>33}, {:id=>23, :name=>"Tom", :score=>50}, {:id=>512, :name=>"Joey", :score=>27}]

#3


1  

If, as in the example, scores[i][:id] = users[i][:id] for all i, and you are using v1.9+ (where key insertion order is maintained), you could write:

如果,如本例中所示,对于所有i,评分[i][:id] = users[i][:id],并且使用v1.9+(保持键插入顺序),您可以这样写:

scores.zip(users).each_with_object({}) do |(sh,uh),h|
   h.update(sh).update(uh)
end

Would I use this? Would you?

我会使用这个吗?你会吗?

#1


3  

Assuming that in users there is always record with corresponding :id from scores:

假设在用户中总是有相应的记录:id来自分数:

scores = [{id: 1, score: 33}, {id: 23, score: 50}, {id:512, score: 27}]
users  = [{id: 1, name: "Jon"}, {id: 23, name: "Tom"}, {id: 512, name: "Joey"}]

scores = scores.map { |score| score.merge(users.find { |user| user[:id] == score[:id] }) }
# => [{:id=>1, :score=>33, :name=>"Jon"}, {:id=>23, :score=>50, :name=>"Tom"}, {:id=>512, :score=>27, :name=>"Joey"}]

Hope that puts you in proper direction!

希望这能给你一个正确的方向!

#2


1  

You can use an intermediate Hash.

可以使用中间哈希。

hsh = Hash[ a1.map {|h| [h[:id], h[:score]]} ]
# => {1=>33, 23=>50, 512=>27}
a2.map {|h| h[:score] = hsh[h[:id]]; h}
# => [{:id=>1, :name=>"Jon", :score=>33}, {:id=>23, :name=>"Tom", :score=>50}, {:id=>512, :name=>"Joey", :score=>27}]

#3


1  

If, as in the example, scores[i][:id] = users[i][:id] for all i, and you are using v1.9+ (where key insertion order is maintained), you could write:

如果,如本例中所示,对于所有i,评分[i][:id] = users[i][:id],并且使用v1.9+(保持键插入顺序),您可以这样写:

scores.zip(users).each_with_object({}) do |(sh,uh),h|
   h.update(sh).update(uh)
end

Would I use this? Would you?

我会使用这个吗?你会吗?