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

[Query] How would I flatten an array inside an object? #7

Closed
bluefangs opened this issue Jun 15, 2017 · 6 comments
Closed

[Query] How would I flatten an array inside an object? #7

bluefangs opened this issue Jun 15, 2017 · 6 comments

Comments

@bluefangs
Copy link

Hi,
This is not an issue. I'd just like to know if this is possible:

I have an object:

{
"ipv4": {
         "enable": "yes",
         "rule": [
             {
                "flow": {
                    "destinationAddress": "0.0.0.0"
                }
            }
        ]
    }
}

I'm looking to produce an output like:

{
       ipv4.enable: yes,
       rule.0.flow.destinationAddress: 0.0.0.0
}

Is this possible?

@dimadeveatii
Copy link
Owner

Hi,

Currently array's are set as they are, meaning no flattening is applied inside array's documents.
You can do something like

var $ = require('mongo-dot-notation')
var data = {
"ipv4": {
         "enable": "yes",
         "rule": $.$(0).$set({
                "flow": {
                    "destinationAddress": "0.0.0.0"
                }
            })
    }
}

var flat = $.flatten(data)

/* ouputs:
{ 
  $set: {
       'ipv4.enable': yes,
       'ipv4.rule.0': { flow: { destinationAddress: '0.0.0.0' } }
  }
}
*/

This will set the first embedded document in the ipv4.rule array, however it will replace entire document rather than just updating the flow.destinationAddress field.

I might consider adding your request as a feature to be added in the next releases.

@bluefangs
Copy link
Author

Thanks for the quick turnaround @dimadeveatii
I happened to find another npm package that fits my use case

@ramirotw
Copy link

hi @bharathkeshav12, I'm interested in that package that you found, do you mind sharing it?

@lsaether
Copy link

Is this still on the radar to be completed eventually? Would be really helpful for a project I'm working on that has a lot of nested objects in arrays, where I need to update deeply nested values.

@dimadeveatii
Copy link
Owner

Hi @lsaether, this feature was not added yet as there was no real demand. Could you please describe your exact usage and need? Is it the same as described in the initial post?

@dimadeveatii
Copy link
Owner

Happy to announce array support has been added.
See unit test example:

it('Should flatten array inside of object', () => {
const data = {
ipv4: {
enable: 'yes',
rule: [
{
flow: {
destinationAddress: '0.0.0.0',
},
},
],
},
};
expect(flatten(data, { array: true })).toStrictEqual({
$set: {
'ipv4.enable': 'yes',
'ipv4.rule.0.flow.destinationAddress': '0.0.0.0',
},
});
});

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

No branches or pull requests

4 participants