I'm trying to parse the array from the url query parameters. The url is as below which contains the array of emp objects specified in query parameters
http://localhost:8080/list?emp[0].id=21&emp[0].name=john&emp[1].id=22&emp[1].name=tom
Can someone please help me with the function that will parse such query params(with some ReGEX maybe) and returns the array as below
[{id:'21',name:'john'},{id:'22',name:'tom'}]
Note: There could be any number of emp objects mentioned in the query params, for example purpose I've just added two. I've looked for similar solutions but none of them seems to return the array which has its objects specified in the query params with the index value. So please don't mark the question as duplicate
Edit: I've written the below function which works for fetching simple query params but doesn't work for array of objects specified in query param as above.
export const getQueryParamByName = (name, url) => {
if (!url) url = window.location.href;
name = name.replace(/[[\]]/g, '\\$&');
const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
const results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}