ruby 中的条件深度合并哈希数组
我有两个数据结构,我想将它们合并在一起 - 据我所知,这称为“深度合并”。
它应该遵循与 Rails 的 深度合并 相同的逻辑,除了我的要求不同之处在于值是仅当它们符合某些条件时才会覆盖目标哈希。
作为一个例子,给定以下两个数据结构:
hash1 = {
"data": [
{"id": "1", "type": "foo", created_at: "<IGNORE>" },
{"id": "2", "type": "bar", created_at: "<IGNORE>" }
],
meta: {
page: 1
}
}
hash2 = {
"data": [
{"id": "1", "type": "baz", created_at: "01.01.2022", name: 'thing' },
{"id": "2", "type": "qux", created_at: "02.01.2022" }
],
meta: {
page: 1
}
}
我想实现以下输出:
irb(main):001:0> hash1.deep_merge(hash2)
=> {
"data": [
{"id": "1", "type": "baz", created_at: "<IGNORE>", name: 'thing' },
{"id": "2", "type": "qux", created_at: "<IGNORE>" }
],
meta: {
page: 1
}
}
在 hash1 中, created_at
的值包含一个标签
,该标签应该规定不应合并 hash2 中的相应值。所有其他键和值都应该合并,就像我使用 Rails 的 deep_merge
一样。
I have two data structures that I would like to merge together - as I understand this is called a 'deep merge'.
It should follow the same logic as Rails' deep merge, except my requirement differs in that values are only overwritten on the target hash if they match certain conditions.
As an example, given the following two data structures:
hash1 = {
"data": [
{"id": "1", "type": "foo", created_at: "<IGNORE>" },
{"id": "2", "type": "bar", created_at: "<IGNORE>" }
],
meta: {
page: 1
}
}
hash2 = {
"data": [
{"id": "1", "type": "baz", created_at: "01.01.2022", name: 'thing' },
{"id": "2", "type": "qux", created_at: "02.01.2022" }
],
meta: {
page: 1
}
}
I would like to achieve the following output:
irb(main):001:0> hash1.deep_merge(hash2)
=> {
"data": [
{"id": "1", "type": "baz", created_at: "<IGNORE>", name: 'thing' },
{"id": "2", "type": "qux", created_at: "<IGNORE>" }
],
meta: {
page: 1
}
}
In hash1, the values for created_at
contain a tag <IGNORE>
that should stipulate that the corresponding value from hash2 should not be merged. All the other keys and values should merge as it would if I was to use Rails' deep_merge
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
免责声明:您没有说条件是什么,尽管如此,我会帮助您将此输入转换为此输出,稍后编辑您的问题并说出条件会很有趣。
我对 ruby 没有太多知识,但我创建了一个简单的算法来合并两个没有重复的哈希结构并保留键的数据类型,它不是
更好的算法或解决方案,但我认为这个实现有助于您找到其他更好的解决方案来解决您的问题
复杂度 O(n)
连接哈希并将键转换为字符串
result = hash1.merge(hash2).transform_keys (&:to_s)
保存元信息
meta = hash1[:meta]
则将符号数组转换为字符串数组
如果哈希没有键名,
将其删除构建新散列 :D
final_result = {"data" =>;结果,元:元}
输出结果
Disclaimer: You didn't say what the conditions are, even so I'll help you transform this input into this output, it would be interesting to edit your question later and say the conditions.
i don't have many knowledge in ruby but i created a simple algorithm for merge two hash structures without duplicates and keep data type of key, it's not
the better algorithm or solution but i think this implemetation help to your a find other better solution for resolver your problem
Complexity O(n)
Join the hashes and transform the keys into a string
result = hash1.merge(hash2).transform_keys(&:to_s)
save meta information
meta = hash1[:meta]
convert array of symbols to array of strings
if the hash does not have the key name remove it
Build new hash :D
final_result = {"data" => result, meta: meta}
output result