0

How can I convert an object like that

{
    param1: "value1",
    param2: "value2",
    param3: ["value31" "value32"]    
}

into a querystring like that:

param1=value1&param2=value2&param3[]=value31&param3[]=value32

to be passed along an http.get reuquest?

Marco Gagliardi
  • 565
  • 1
  • 8
  • 24
  • [This may help](https://stackoverflow.com/questions/14525178/is-there-any-native-function-to-convert-json-to-url-parameters) – DDiVita Jul 14 '17 at 11:08
  • well, I was actually looking for a sort of native Angular API to do that, rather than a custom parsing function. Also, I have no intention of using jquery! – Marco Gagliardi Jul 14 '17 at 11:12

1 Answers1

1

I think this is something that you are looking for:

import { URLSearchParams } from '@angular/http';

let someObject = {
  param1: "value1",
  param2: "value2",
  param3: ["value31" "value32"]    
}

let queryString = new URLSearchParams();

for (const key in someObject) {
  queryString.set(key, someObject[key]);
}

queryString.toString();

You can find out more here URLSearchParams API about the different options.

onetwo12
  • 2,359
  • 5
  • 23
  • 34