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

Possible to keep parent key #116

Open
yashinnhl opened this issue Nov 5, 2020 · 3 comments
Open

Possible to keep parent key #116

yashinnhl opened this issue Nov 5, 2020 · 3 comments

Comments

@yashinnhl
Copy link

yashinnhl commented Nov 5, 2020

Hi I want to keep parent key and all its children as stringified JSON, is it possible to do that with flatten?

Example

{
     a : 'blah',
     data : {
        a : 'sth',
        b: 'sth'
    }
}

expected output

{
a: 'blah', 
   data: `{
     a: sth,
     b: sth
   }`,
  data.a = sth,
  data.b = sth
}

I really appreciate if you can guide me here? Thanks

@yashinnhl
Copy link
Author

yashinnhl commented Nov 5, 2020

We can add key and value when isObject is true.

if (isobject) {
        // add first level keys example: data , data.object
        output[newKey] = value;
}

@giorgosera
Copy link

Hey @yashinnhl ! I'm also interested in this.
I'm not sure I understood your second comment! What do you mean by dictionary?

@yashinnhl
Copy link
Author

Hey @yashinnhl ! I'm also interested in this.
I'm not sure I understood your second comment! What do you mean by dictionary?

Hey @giorgosera I edited my comments. Sorry for confusion. In general, I mean we can keep first level keys just like an example even if they are object.
consider this :

ExampleObject:  {
 a : 'blah',
 data : {
   a : 'sth',
   b: 'sth'
 }
}

Can be converted to :

 {
  a: 'blah',
  data: { a: sth, b: sth },
  data.a = sth,
  data.b = sth
}

To achieve this, we need to add condition - if (isObject) - to the index, and output[newKey] = value will keep parent key for us.
For example, value = { a: sth, b: sth } will be assigned data key first and then flatten process will create data.a = sth, and data.b = sth.

I hope it helps. I am still not sure it is optimized or not but at least worked for me :).

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

2 participants