Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struggling with creating deep nested hash #4

Open
rohandey opened this issue Jun 1, 2019 · 0 comments
Open

Struggling with creating deep nested hash #4

rohandey opened this issue Jun 1, 2019 · 0 comments

Comments

@rohandey
Copy link

rohandey commented Jun 1, 2019

I am struggling with this for couple of days, not able to form the desired deep nested hash, hope to get some help.
Input hash:

@data = { 'nodes' => { 
                'a' => {
                  'id' => 'a',
                  'parent_id' => 'root',
                  'children' => {
                    'a1' => {
                      'id' => 'a1',
                      'parent_id' => 'a',
                      'children' => { }
                    }
                  }
                },

                'b' => {
                  'id' => 'b',
                  'parent_id' => 'a',
                  'children' => {}
                },

                'c' => {
                  'id' => 'c',
                  'parent_id' => 'b',
                  'children' => {}
                },

                'b2' => {
                  'id' => 'b2',
                  'parent_id' => 'b',
                  'children' => {}
                }

              }}

Desired result:


@h = [{ 'nodes' => { 
                'a' => {
                  'id' => 'a',
                  'parent_id' => 'root',
                  'children' => {
                    'a1' => {
                      'id' => 'a1',
                      'parent_id' => 'a',
                      'children' => {}
                    },
                    'b' => {
                      'id' => 'b',
                      'parent_id' => 'a',
                      'children' => {
                        'c' => {
                          'id' => 'c',
                          'parent_id' => 'b',
                          'children' => {}
                        },

                        'b2' => {
                          'id' => 'b2',
                          'parent_id' => 'b',
                          'children' => {}
                        }
                      }
                    }
                  }
                }
              }}]    
      

What I am trying is this


@data['nodes'].each do |key, value| 
      if key == 'a'
        @h[0] = { 'a' => value }
      else 
        parent_key = value['parent_id']
        key_scope = Xf.scope(parent_key)
        
       @h.map(&key_scope.set! do |h, k, v| 
          if v.present?
            v['children'].update({ key => value })
          end
        end)
      end
    end 

Input @DaTa['nodes'] can have over 1000 elements and output can get nested to level n

Any help will be greatly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant