0

I need delete all key created_at and updated_at in array of hashs . My hash looks like :

assessment_with_desc = {
  "id": 1,
  "name": "First assessment",
  "created_at": "2020-03-14T20:13:27.006Z",
  "updated_at": "2020-03-14T20:13:27.006Z",
  "description_with_child_models": [
    {
      "id": 3,
      "title": "First category",
      "created_at": "2020-02-20T15:32:46.379Z",
      "updated_at": "2020-03-14T20:16:11.530Z",
      "accessment_id": 1,
      "sub_categories": [
      {
        "id": 1,
        "title": "First sub_category",
        "category_id": 3,
        "created_at": "2020-02-20T15:40:49.793Z",
        "updated_at": "2020-02-20T15:40:49.793Z",
        "stages": [
        {
          "id": 5,
          "title": "First stage",
          "sub_category_id": 1,
          "created_at": "2020-02-20T15:44:10.603Z",
          "updated_at": "2020-02-20T15:44:10.603Z"
        }
       ]
      }
     ]
    }
  ]
}

I did it, but it work only in this case assessment_with_desc.delete('created_at') and assessment_with_desc.delete('updated_at'):

assessment_with_desc.delete('created_at')
assessment_with_desc.delete('updated_at')
assessment_with_desc['description_with_child_models'].delete('created_at')
assessment_with_desc['description_with_child_models'].delete('updated_at')
assessment_with_desc['description_with_child_models'][0]['sub_categories'].delete('created_at')
assessment_with_desc['description_with_child_models'][0]['sub_categories'].delete('updated_at')
assessment_with_desc['description_with_child_models'][0]['sub_categories'][0]['stages'].delete('created_at')
assessment_with_desc['description_with_child_models'][0]['sub_categories'][0]['stages'].delete('updated_at')
1
  • Welcome to Stack Overflow! I’m not sure what you’re trying to do, here. You do seem to be trying to remove a lot of values, tho. Perhaps you could select the keys you want to keep using Hash#slice, instead? Commented Mar 17, 2020 at 10:15

1 Answer 1

1

You can recursively invoke a function to remove the key/value in case the key is equal to :created_at or :updated_at:

def recursively_delete_timestamps(object)
  object.transform_values do |value|
    next value unless value.is_a?(Array)

    value.map do |inner_hash|
      recursively_delete_timestamps(inner_hash)
    end
  end.reject do |key, _|
    key.in?(%i[created_at updated_at])
  end
end

recursively_delete_timestamps(assessment_with_desc)
# {:id=>1,
#  :name=>"First assessment",
#  :description_with_child_models=>
#   [{:id=>3,
#     :title=>"First category",
#     :accessment_id=>1,
#     :sub_categories=>
#      [{:id=>1,
#        :title=>"First sub_category",
#        :category_id=>3,
#        :stages=>[{:id=>5, :title=>"First stage", :sub_category_id=>1}]}]}]}

Notice, the hash remains unchanged. Nor transform_values, map or reject modify the original object, but return a new one.

Sign up to request clarification or add additional context in comments.

3 Comments

What is this .in? ?
It's a Rails method to check if the receiver is included in the object passed as a parameter.
Oh okay. I am knowing rails that's why the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.