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 string with brackets #221

Closed
sebsobseb opened this issue Nov 7, 2019 · 2 comments
Closed

query string with brackets #221

sebsobseb opened this issue Nov 7, 2019 · 2 comments

Comments

@sebsobseb
Copy link

Hi,

I trying to convert my filter object

{
    "type": 1,
    "from": "2019-11-04",
    "until": "2019-11-13",
    "status": "blocked"
}

To this format:

?filter[type]=1&filter[from]=2019-11-04&filter[until]=2019-11-13&filter[status]=blocked

Is there a way to achieve this?

Thanks!

@lucasassisrosa
Copy link

@sebsobseb you could do

const params = {
    "filter[type]": 1,
    "filter[from]": "2019-11-04",
    "filter[until]": "2019-11-13",
    "filter[status]": "blocked"
}

qs.stringify(params);

@sebsobseb
Copy link
Author

@lucasassisrosa your solution works, thanks.
With help of Lodash i ended up with this solution:

return qr.stringify(_.chain(filterObject)
    // Remove null values
    .omitBy(_.isNull)
    // Wrap filter keys with 'filter[]'
    .mapKeys((filterValue, filterKey) => {
        return `filter[${filterKey}]`;
    })
    .value()
);

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