How to convert a query string to an object in TypeScript

1 Answer

0 votes
const queryString = "?q=TypeScript&lang=en&limit=10";

let obj = new URLSearchParams(queryString);

console.log(obj.get('q')); 
console.log(obj.get('lang'));
console.log(obj.get('limit'));

obj = Object.fromEntries(new URLSearchParams(queryString));

console.log(obj);

  
  
  
  
/*
run:
  
TypeScript
en
10
{ q: 'TypeScript', lang: 'en', limit: '10' }
  
*/

 



answered Apr 22, 2022 by avibootz

Related questions

3 answers 320 views
1 answer 249 views
1 answer 197 views
3 answers 424 views
3 answers 330 views
1 answer 197 views
...