javascript - How to log the result of "new URLSearchParams()"? - Stack Overflow

I am looking at this example on the google chrome docs.I am trying to console.log the variable params

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

I am trying to console.log the variable params

let url = new URL(';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?

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.?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?

Share Improve this question edited Apr 5, 2022 at 17:15 piraha asked Apr 4, 2022 at 18:25 pirahapiraha 1351 silver badge15 bronze badges 3
  • Didn't understand if this is what you mean but you have to iterate throw the params variable in order to see the results (as shown in the example you shared). It appears as empty due to is not an object and it does not work the same way, params is an instance of URLSearchParams. – Luka Cerrutti Commented Apr 4, 2022 at 18:31
  • When you loop over params with a for loop, you only see enumerable properties. However the object contains other properties that are not enumerable, but you do see them when you console.log the whole object. – James Commented Apr 4, 2022 at 19:08
  • Ah @LukaCerrutti that's what I didn't get, that params isn't really an object, but an instance of URLSearchParams. I thought that if I can loop through it like an object, it then must be an object. – piraha Commented Apr 5, 2022 at 17:17
Add a ment  | 

2 Answers 2

Reset to default 4

You can use fromEntries

let url = new URL('https://example.?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'}

The way I got this to work was:

  1. Go to the following url: https://example./?foo=1&bar=2
  2. Open dev tools in Chrome (or your favorite browser) and drop the following code into the console:

let url = window.location.search;

let searchParams = new URLSearchParams(url);

let foo = searchParams.get('foo');
let bar = searchParams.get('bar');

console.log(foo, bar)

Output should be 1 2

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745326100a4622663.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信