Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一些哈希数组
a = [{name:"x", long:1.0, lat:2.0}, {name:"y", long:2.0, lat:3.0}, {name:"z", long:1.0, lat:2.0}]
如何删除{name:"x", long:1.0, lat:2.0},哪些坐标等于最后一个元素,换句话说,我需要留下最后一个(在我的情况下:with name:"z")具有唯一坐标的散列并删除所有具有相同坐标的先前元素
{name:"x", long:1.0, lat:2.0}
name:"z"
尝试使用Array#uniq块:
Array#uniq
a.uniq { |item| [item[:lat], item[:long]] }
块的返回值用作比较唯一性的值。
目前尚不清楚为什么要删除“x”而不是“z”,但您可以通过在调用数组之前反转数组来使用示例数据集来实现这uniq一点。
uniq