比较两个哈希值,如果 ID 不存在则删除

发布于 2024-12-01 22:22:15 字数 1680 浏览 0 评论 0原文

我有两个哈希值,一个看起来像这样:

{:id => "SG_5viWPcG0SLvszXbBxogLkT_51.514568_-0.126244@1300740367",
 :name => "Shellys Shoes",
 :lat => 51.5145683289,
 :lng => -0.1262439936}

这只是一条记录,大约有 80 条,

我的第二个哈希值是:

{"id":"SG_2zNWLdG9147g2ROvNWpDHr_51.512360_0.124480@1300740823",
"lat":51.5123596191,
"lng":-0.1244800016}

上面的哈希值也只是一条记录,但是它是上面的哈希值经过一个API不返回所有记录,仅返回有效记录,我想要做的是将顶部哈希与底部哈希进行比较,并删除底部哈希中不存在的任何记录,

例如,如果id:SG_5viWPcG0SLvszXbBxogLkT_51.514568_-0.126244@1300740367 不在第二个哈希中,然后删除该记录,

我可以比较哈希,但看不到如果 ID 不存在如何删除?

谢谢你们!

编辑: 这些是返回值...

{"points":[{"id":"SG_75oKOgvgFPLjwmdyAKA2rq_51.512825_-0.124655@1300740283","lat":51.5128250122,"lng":-0.1246550009},{"id":"SG_0Sz9CBF5t70tdAffTKYNSg_51.512360_-0.124388@1300740807","lat":51.5123596191,"lng":-0.1243880019},{"id":"SG_2zNWLdG9147g2ROvNWpDHr_51.512360_-0.124480@1300740823","lat":51.5123596191,"lng":-0.1244800016},{"id":"SG_5PvBx89sLPgplapegVJDFv_51.513100_-0.124809@1300740049","lat":51.5130996704,"lng":-0.1248089969},{"id":"SG_4luyHFi5R2f1w3cpjT61ik_51.513393_-0.124556@1300740719","lat":51.5133934021,"lng":-0.1245559976},{"id":"SG_4luyHFi5R2f1w3cpjT61ik_51.513393_-0.124556@1300740719","lat":51.5133934021,"lng":-0.1245559976},{"id":"SG_0pEvrpt7bs42jPAxFSrquC_51.512264_-0.124413@1300740807","lat":51.5122642517,"lng":-0.1244129986},]}

这是原始格式:

[ {         :id => "SG_2Km6LX3tEcFwx24eotTHIY_51.513016_-0.123721@1300740411",
            :name => "French Connection Group Plc",
             :lat => 51.5130157471,
             :lng => -0.1237210035
}]

I have two hashes, one looks like this:

{:id => "SG_5viWPcG0SLvszXbBxogLkT_51.514568_-0.126244@1300740367",
 :name => "Shellys Shoes",
 :lat => 51.5145683289,
 :lng => -0.1262439936}

This is just one record, there are about 80,

The second hash I have is:

{"id":"SG_2zNWLdG9147g2ROvNWpDHr_51.512360_0.124480@1300740823",
"lat":51.5123596191,
"lng":-0.1244800016}

The hash above is again just one record, however it is a product of the HASH above after going through an API that does not return all the records, only the valid ones, what I want to do is compare the top hash with the bottom one and delete any records that are not present in the bottom hash,

for example if id:SG_5viWPcG0SLvszXbBxogLkT_51.514568_-0.126244@1300740367 is not in the the second hash then delete that record,

I can compare the hashes, but cant see how to delete if ID is not present?

Thanks guys!

edit:
these are the returned values....

{"points":[{"id":"SG_75oKOgvgFPLjwmdyAKA2rq_51.512825_-0.124655@1300740283","lat":51.5128250122,"lng":-0.1246550009},{"id":"SG_0Sz9CBF5t70tdAffTKYNSg_51.512360_-0.124388@1300740807","lat":51.5123596191,"lng":-0.1243880019},{"id":"SG_2zNWLdG9147g2ROvNWpDHr_51.512360_-0.124480@1300740823","lat":51.5123596191,"lng":-0.1244800016},{"id":"SG_5PvBx89sLPgplapegVJDFv_51.513100_-0.124809@1300740049","lat":51.5130996704,"lng":-0.1248089969},{"id":"SG_4luyHFi5R2f1w3cpjT61ik_51.513393_-0.124556@1300740719","lat":51.5133934021,"lng":-0.1245559976},{"id":"SG_4luyHFi5R2f1w3cpjT61ik_51.513393_-0.124556@1300740719","lat":51.5133934021,"lng":-0.1245559976},{"id":"SG_0pEvrpt7bs42jPAxFSrquC_51.512264_-0.124413@1300740807","lat":51.5122642517,"lng":-0.1244129986},]}

This is the original format:

[ {         :id => "SG_2Km6LX3tEcFwx24eotTHIY_51.513016_-0.123721@1300740411",
            :name => "French Connection Group Plc",
             :lat => 51.5130157471,
             :lng => -0.1237210035
}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白首有我共你 2024-12-08 22:22:15

您可以从返回的值中收集有效 ID 的列表,如下所示:

valid_ids = returned["points"].collect { |point| point["id"] }

然后,您可以使用以下方式从原始值中删除无效值:

original.delete_if { |entry| !valid_ids.include? entry[:id] }

You can collect a list of the valid IDs from the returned values with something like:

valid_ids = returned["points"].collect { |point| point["id"] }

You can then remove the invalid values from the original with something like:

original.delete_if { |entry| !valid_ids.include? entry[:id] }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文