/    Sign up×
Community /Pin to ProfileBookmark

Create url params from javascript object

Hello. Here is javascript object convert to url params issue.

“`
var urlParams = {
timezone : ‘EET’,
indicators: [‘black sea’, ‘red roses’]
};
“`

When run in console with jquery:

` console.log($.param(urlParams, true));`

Result is wrong:

`timezone=EET&indicators=black%20sea&indicators=red%20roses&indicators=john`

Problem is that param “indicators” repeated.
How to display the parameter only once and separate the array (indicators) elements with a comma?
Comma is %2C
Like this:

`timezone=EET&indicators=black%20sea%2Cred%20roses%2Cjohn`

Thank you.

to post a comment
Full-stack DeveloperJavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMay 13.2022 — >How to display the parameter only once and separate the array (indicators) elements with a comma?

The function `join` can help you to do so:

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/join

Note that the values must not contain a comma when doing so.
Copy linkTweet thisAlerts:
@VictorJohnsonandroniMay 20.2022 — Here you go:
``<i>
</i>function encodeQueryData(data) {
const ret = [];
for (let d in data)
ret.push(encodeURIComponent(d) + '=' + encodeURIComponent(data[d]));
return ret.join('&amp;');
}<i>
</i>
`</CODE>
Usage:
<CODE>
`<i>
</i>const data = { 'first name': 'George', 'last name': 'Jetson', 'age': 110 };
const querystring = encodeQueryData(data);<i>
</i>
``
Copy linkTweet thisAlerts:
@SempervivumMay 20.2022 — @VictorJohnsonandroni#1644145

Please use code tags when posting code: `your code here` or triple backticks. I edited your posting accordingly.

Your posting doesn't answer the TO's question which reads:
>How to display the parameter only once and separate the **array** (indicators) elements with a comma?

The TO is already using the jQuery function $.param which is encoding the query string. No benefit in coding this manually. And javascript provides the URLSearchParams API which is doing the same:

https://stackoverflow.com/questions/316781/how-to-build-query-string-with-javascript

@bumbar When testing the URLSearchParams API I noticed that, unlike $.param, it converts an array to a comma separated string already just as you require. Therefore I recommend to use this instead of $.param.
Copy linkTweet thisAlerts:
@wajahi9045Aug 06.2022 — good code
×

Success!

Help @bumbar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...