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 316 views
1 answer 247 views
1 answer 195 views
3 answers 422 views
3 answers 326 views
1 answer 193 views
...