我想呼籲後續JSON的uniq的方法,以便它只會在EMPLOYEE_ID返回唯一結果的基礎如何使用Ruby的uniq在嵌套數組/哈希
# Json array
a ={
results: [
{
employee: {
name: "A",
employee_id: "A-00016",
title: 1
}
},{
employee: {
name: "A",
employee_id: "A-00016",
title: 2
}
},{
employee: {
name: "C",
employee_id: "C-00017",
title: 3
}
}
]
}
# Calling uniq on a
a.uniq { |p| p.values_at(:employee_id) }
不過,我只得到這個結果
{
results: [
{
employee: {
name: "A",
employee_id: "A-00016",
title: 1
}
}
]
}
而不是我想要的
{
results: [
{
employee: {
name: "A",
employee_id: "A-00016",
title: 1
},{
employee: {
name: "C",
employee_id: "C-00017",
title: 3
}
}
]
}
現在用正確的方法來輸出的I我想要的結果?
我會推薦做:'a [:results] .uniq!',但是第一個實例中title = 1,第二個中title = 2。所以,紅寶石會認爲這兩個不同。 – oliviergg