繁体   English   中英

删除嵌套在 rails 上的对象数组 Ruby 中的属性

[英]delete attributes nested within an array of objects Ruby on rails

我需要大量删除attr7_nested中的属性nested1 ,我不需要将这些数据保存在银行中,只需将其存储在变量中以发送到日志文件。

示例文件如下:

[{
  "attr1": 120,
  "attr2": 24,
  "attr3": 11400,
  "attr4": "Caixa",
  "attr5": 2000000,
  "attr6": 1744000,
  "attr7_nested": {
    "nested1": 1,
    "nested2": "Essential",
    "nested3": "med",
    "nested4": "Med"
  }
},
{
  "attr1": 120,
  "attr2": 24,
  "attr3": 11400,
  "attr4": "Caixa",
  "attr5": 2000000,
  "attr6": 1744000,
  "attr7_nested": {
    "nested1": 1,
    "nested2": "Ess",
    "nested3": "med",
    "nested4": "Med"
  }
}]

array是包含问题中的嵌套散列的数组时,以下内容将从所有嵌套的attr7_nested散列中删除nested1键:

array.each { |hash| hash[:attr7_nested].delete(:nested1) }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM