1

Following is the code which is working fine for me, the only problem here I can see that I need to call slice(0, -1) on my string to remove & from the last. Let me know if there is a better and efficient way of writing this or this is also acceptable.

Thanks in advance.

Code -

const object1 = {
  a: 'somestring',
  b: 42,
  c: '',
  d: "test1",
  e: null,
  f: 'test2'
};

let str = '';
for (const [key, value] of Object.entries(object1)) {
  str += `${key}=${value}&`
}

const paramStr = str.slice(0, -1);

console.log(paramStr);
Nesh
  • 2,389
  • 7
  • 33
  • 54
  • 1
    Is the last letter always &? – Raffobaffo Jul 12 '20 at 21:09
  • @Raffobaffo yes – Nesh Jul 12 '20 at 21:11
  • 2
    Since your current code seems to be working and you are seeking for a more efficient way to achieve the same result, I’m voting to close this question because this question belongs on [Code Review](https://codereview.stackexchange.com/), another site in the Stack Exchange network . – AndrewL64 Jul 12 '20 at 21:12
  • it seems you already got the best solution: https://stackoverflow.com/questions/952924/javascript-chop-slice-trim-off-last-character-in-string – ddfra Jul 12 '20 at 21:12
  • Does this answer your question? [JavaScript chop/slice/trim off last character in string](https://stackoverflow.com/questions/952924/javascript-chop-slice-trim-off-last-character-in-string) – Raffobaffo Jul 12 '20 at 21:14
  • As @ddfra notes there is already an answer to the question. The tile that describes the op wants to remove the last & is misleading – Raffobaffo Jul 12 '20 at 21:30
  • I agree with @Raffobaffo, you should change the title in "clean way to create a querystring from an object", which already has an answer here https://stackoverflow.com/questions/1714786/query-string-encoding-of-a-javascript-object although it is 10 years old and probably does not use new empowerements – ddfra Jul 12 '20 at 21:33

3 Answers3

4

I think doing the slice is fine. There are numerous other ways you could approach not having it. For example, you could map and then use .join('&'):

const object1 = {
  a: 'somestring',
  b: 42,
  c: '',
  d: "test1",
  e: null,
  f: 'test2'
};

console.log(Object.entries(object1).map(([k,v]) => `${k}=${v}`).join('&'))
Anthony
  • 6,422
  • 2
  • 17
  • 34
3

Simply map and join, that will get rid of the trailing &:

let paramStr = Object.entries(object1).map(([key, value]) => key + "=" + value).join("&");

Demo:

const object1 = {
  a: 'somestring',
  b: 42,
  c: '',
  d: "test1",
  e: null,
  f: 'test2'
};

let paramStr = Object.entries(object1).map(([key, value]) => key + "=" + value).join("&");

console.log(paramStr);
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
  • 1
    Thx a lot for your answer..I will accept it inn 4 min :) .. don't know why someone down voted this – Nesh Jul 12 '20 at 21:17
0

If you are using lodash, you can consider utilizing the trimEnd function.

It can remove specified characters from the end of a string if you pass them as a second parameter.

For example:

const paramStr = _.trimEnd('a=somestring&b=42&c=&d=test1&e=null&f=test2&', '&');

console.log(paramStr);
// "a=somestring&b=42&c=&d=test1&e=null&f=test2"
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.19/lodash.min.js"></script>
Marian13
  • 7,740
  • 2
  • 47
  • 51