ruby 中的条件深度合并哈希数组

发布于 2025-01-11 12:28:12 字数 1109 浏览 0 评论 0原文

我有两个数据结构,我想将它们合并在一起 - 据我所知,这称为“深度合并”。

它应该遵循与 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 技术交流群。

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

发布评论

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

评论(1

你げ笑在眉眼 2025-01-18 12:28:12

免责声明:您没有说条件是什么,尽管如此,我会帮助您将此输入转换为此输出,稍后编辑您的问题并说出条件会很有趣。

我对 ruby​​ 没有太多知识,但我创建了一个简单的算法来合并两个没有重复的哈希结构并保留键的数据类型,它不是
更好的算法或解决方案,但我认为这个实现有助于您找到其他更好的解决方案来解决您的问题

复杂度 O(n)

连接哈希并将键转换为字符串

result = hash1.merge(hash2).transform_keys (&:to_s)

保存元信息

meta = hash1[:meta]

则将符号数组转换为字符串数组

`result = result["data"].map do |res|
   {
     'id' => res[:id].to_s,
     'type' => res[:type].to_s,
     'created_at' => res[:created_at].to_s,
     'name' => res[:name].to_s
    }
  end
`

如果哈希没有键名,

`
result =  
  result.map { 
    |res| res["name" == ""] ? {
      'id' => res["id"],
      'type' => res["type"],
      'created_at' => res["created_at"]
    } : res
  }
`

将其删除构建新散列 :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

`result = result["data"].map do |res|
   {
     'id' => res[:id].to_s,
     'type' => res[:type].to_s,
     'created_at' => res[:created_at].to_s,
     'name' => res[:name].to_s
    }
  end
`

if the hash does not have the key name remove it

`
result =  
  result.map { 
    |res| res["name" == ""] ? {
      'id' => res["id"],
      'type' => res["type"],
      'created_at' => res["created_at"]
    } : res
  }
`

Build new hash :D

final_result = {"data" => result, meta: meta}

output result

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