简体   繁体   中英

delete attributes nested within an array of objects Ruby on rails

I need to remove the attribute nested1 that is inside the attr7_nested in a massive way, I don't need to persist this data in the bank, just store it in a variable to send to a log file.

example file follows:

[{
  "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"
  }
}]

When array is the array containing the nested hashes from your question than the following would remove the nested1 keys from all nested attr7_nested hashes:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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