Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log the result of "new URLSearchParams()"?

I am looking at this example on the google chrome docs.

I am trying to console.log the variable params

let url = new URL('https://example.com?foo=1&bar=2'); // or construct from window.location

let params = new URLSearchParams(url.search.slice(1));

However, this is all I get:

getAll: Object {  }, has: Object {  }, set: Object {  }, sort: Object {  }, 
toString: Object {  }, entries: Object {  }, forEach: Object {  }, 
keys: Object {  }, values: Object {  } }

Like in the example, you can loop through the params variable and console.log each parameter, but not the variable params. Why can't it be logged and seen as an object?

like image 696
piraha Avatar asked Oct 15 '25 21:10

piraha


1 Answers

You can use fromEntries

let url = new URL('https://example.com?foo=1&bar=2'); // or construct from window.location
let params = new URLSearchParams(url.search.slice(1));
console.log(Object.fromEntries(params)) // outputs {foo: '1', bar: '2'}
like image 193
tony Avatar answered Oct 18 '25 13:10

tony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!